admin管理员组文章数量:1297122
In the following example:
<html>
<head>
<SCRIPT language="JavaScript">
function checkcheckboxElem(checkboxElem)
{
alert ("checkboxElem " + checkboxElem.length);
}
</script>
</head>
<body>
<form name="checkForm">
Master: <input type="checkbox" name="master" value="6694" onclick="checkcheckboxElem(document.checkForm.checkboxElem)"><br>
Child1: <input type="checkbox" name="checkboxElem" value="2">
</form>
</body>
The alert outputs 'undefined'. For some reason it does this if there is only one checkbox. If i add another checkbox then it outputs the correct length. If i change it as shown below then it outputs 2
<html>
<head>
<SCRIPT language="JavaScript">
function checkcheckboxElem(checkboxElem)
{
alert ("checkboxElem " + checkboxElem.length);
}
</script>
</head>
<body>
<form name="checkForm">
Master: <input type="checkbox" name="master" value="6694" onclick="checkcheckboxElem(document.checkForm.checkboxElem)"><br>
Child1: <input type="checkbox" name="checkboxElem" value="2">
Child2: <input type="checkbox" name="checkboxElem" value="1">
</form>
Why does it return 'undefined' if it is only one checkbox?
Thanks
In the following example:
<html>
<head>
<SCRIPT language="JavaScript">
function checkcheckboxElem(checkboxElem)
{
alert ("checkboxElem " + checkboxElem.length);
}
</script>
</head>
<body>
<form name="checkForm">
Master: <input type="checkbox" name="master" value="6694" onclick="checkcheckboxElem(document.checkForm.checkboxElem)"><br>
Child1: <input type="checkbox" name="checkboxElem" value="2">
</form>
</body>
The alert outputs 'undefined'. For some reason it does this if there is only one checkbox. If i add another checkbox then it outputs the correct length. If i change it as shown below then it outputs 2
<html>
<head>
<SCRIPT language="JavaScript">
function checkcheckboxElem(checkboxElem)
{
alert ("checkboxElem " + checkboxElem.length);
}
</script>
</head>
<body>
<form name="checkForm">
Master: <input type="checkbox" name="master" value="6694" onclick="checkcheckboxElem(document.checkForm.checkboxElem)"><br>
Child1: <input type="checkbox" name="checkboxElem" value="2">
Child2: <input type="checkbox" name="checkboxElem" value="1">
</form>
Why does it return 'undefined' if it is only one checkbox?
Thanks
Share Improve this question asked Oct 11, 2011 at 11:25 ziggyziggy 15.9k69 gold badges198 silver badges291 bronze badges5 Answers
Reset to default 5In the first case document.checkForm.checkboxElem
refers to a single input element, in the second case it refers to an array of input elements. You could do:
function checkcheckboxElem(checkboxElem) {
if(!checkboxElem.length)
checkboxElem = [checkboxElem];
alert ("checkboxElem " + checkboxElem.length);
}
It returns undefined
because there is only a single element - javascript does not treat this as an array so it has no length property .... you could use this to ensure you always have a length :
var len = checkboxElem.length;
if(len == undefined) len = 1;
In the first case it reffers directly to the checkbox element and it has no length property. In the second case, there are multiple elements with the same name, so it returns an array of corresponding elements.
To get array of elements use
document.getElementsByName('checkboxElem');
It returns the number of elements found, instead of the value of one..
The output 2 isn't the value of one of the boxes.. It's the amount of elements..
use something like checkboxElem.val()
beacuse in first case
document.checkForm.checkboxElem
is specific element, but if there are more elements with same name it is array - try this
alert ("checkboxElem " + ( typeof( checkboxElm.length ) == 'undefined' ? 1 : checkboxElem.length ));
本文标签: htmlJavascript returning 39undefined39 if i have less than one element in the formStack Overflow
版权声明:本文标题:html - Javascript returning 'undefined' if i have less than one element in the form. - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741643235a2390039.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论