これが私のコードです:
HTML:
<form name="frm1">
<input type="checkbox" id="chk1" name="chk1[]" value="1"/>
<input type="checkbox" id="chk1" name="chk1[]" value="2"/>
</form>
<button class="test1">Test 1</button>
<form name="frm2">
<input type="checkbox" id="chk2" name="chk2[]" value="1"/>
<!--<input type="checkbox" id="chk2" name="chk2[]" value="2"/>-->
</form>
<button class="test2">Test 2</button>
<form name="frm3">
<input type="checkbox" name="chk3[]" value="1"/>
<input type="checkbox" name="chk3[]" value="2"/>
</form>
<button class="test3">Test 3</button>
脚本:
$('button.test3').click(function (){
alert(document.frm3.chk3.length);// Gives error: document.frm3.chk3 is undefined
});
$('button.test2').click(function (){
alert(document.frm2.chk2.length);// Outputs: undefined
});
$('button.test1').click(function (){
alert(document.frm1.chk1.length);// Gives 2 as expected
});
フィドル: http://jsfiddle.net/mgsvy/
form
byname
にアクセスできる場合、上記で予期しない結果が生じるのelements
はなぜですか?