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 badges
Add a ment  | 

5 Answers 5

Reset to default 5

In 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