javascript 如何在java脚本中获取CheckBox数组长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8340299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can get CheckBox Array length in java script
提问by java_dev
in my jsp form i have one checkbox Array in my form(jsp and struts).i want to know the length of checkbox Array in java script. ex: <input:checkbox property="chkbox[]"/>
. in javascript i am calling document.form[0].chkbox[].value.length
but it showing javaScript error null or undefined .
在我的jsp表单中,我的表单中有一个复选框数组(jsp和struts)。我想知道java脚本中复选框数组的长度。例如:<input:checkbox property="chkbox[]"/>
。在 javascript 中,我正在调用,document.form[0].chkbox[].value.length
但它显示 javaScript 错误 null 或 undefined 。
回答by Zain Khan
var checkbox = document.getElementsByName('a_checkbox')
var ln = checkbox.length
line 1 get all the active checked boxes line 2 in the length of that array
第 1 行获取该数组长度中第 2 行的所有活动复选框
回答by sushil bharwani
if this the html
如果这是 html
<form name='form1' >
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
</form>
javascript for that length would be
那个长度的javascript将是
alert(document.form1.cbox.length);
alert(document.form1.cbox.length);
or you can also use.
或者你也可以使用。
document.getElementsByName('cbox').length
document.getElementsByName('cbox').length