Javascript about looping checkbox
Javascript about looping checkbox
只是想问一下,为什么下面的代码无效(发现错误)
for(var i=1; i<10; i++)
{
alert(window.document.FORM.checkbox[i].checked);
}
但是我可以执行以下操作,我只想循环在HTML中创建的复选框:
for(var i=1; i<10; i++)
{
alert(window.document.FORM.checkbox1.checked);
}
你的两个陈述不一样;当我等于 1 时,你的陈述将被评估为
alert(window.document.FORM.checkbox[1].checked);
这与
alert(window.document.FORM.checkbox1.checked);
所以确实,要做到这一点,请使用Lee Taylor的解决方案,或者使用id重写整个方法,或者通过document.getElementsByTagName获取所有复选框,例如
var chks = document.getElementsByTagName("input");
for (var i=0, l=chks.length; i<l; i++) {
if ( chks[i].type === "checkbox" )
alert(chks[i].checked);
}
呵呵,博瓦科
很奇怪,但你的代码对我有用。
for(var i=1; i<10; i++)
{
alert(window.document.FORM.checkbox[i].checked);
}
<form name="FORM">
<input type="checkbox" name="checkbox" checked="true" />
<input type="checkbox" name="checkbox" checked="true" />
<input type="checkbox" name="checkbox" />
<input type="checkbox" name="checkbox" checked="true" />
<input type="checkbox" name="checkbox" />
<input type="checkbox" name="checkbox" />
<input type="checkbox" name="checkbox" />
<input type="checkbox" name="checkbox" checked="true" />
<input type="checkbox" name="checkbox" />
<input type="checkbox" name="checkbox" />
</form>
相关文章:
- 根据CHECKBOX输入值动态更新DIV信息
- 如何使用Jquery从Asp.netMVC中的CheckBox中获取选定的值
- 对checkbox的更改事件使用jquery.conf函数
- 具有多个参数的checkbox函数调用
- check if checkbox:check不起作用,why,jquery
- checkbox options=选择选项-angularjs
- jQuery to pass Checkbox Update mysql
- 无法在“(”input:checkbox [name=type]:checked“).each(function())”
- Jquery validation if checkbox checked text box得到class=“”;表单输
- checkbox.attr(“checked”),就像句子子句一样
- 如何在javascript中添加一个具有checkbox属性的变量,该变量将填充view.php中的一个表
- 如何在javascript中设置asp:checkbox的可见性
- jQuery-如何使var文本可点击,该文本放置在由更多元素组成的另一个var中(var,checkbox)
- 选中一个复选框后,使用html.checkbox自动禁用其他复选框
- Angularjs ng-show not working with checkbox with default val
- jQuery.validator rule for not checked checkbox
- Bootstrap checkbox
- checkbox.checked在onclick函数内部总是返回true
- javascript checkbox/uncheckbox同一分区中的多个项目
- Javascript about looping checkbox