简体   繁体   English

如何计算表单中的所有复选框(Javascript)

[英]how to count all check boxes in a form (Javascript)

I have number of check boxes that gets generated dynamically. 我有多个动态生成的复选框。 So i do not know how many check boxes gets generated each time. 所以我不知道每次生成多少个复选框。 I need to have some JavaScript ways to count the total numbers of check boxes in a form. 我需要一些JavaScript方法来计算表单中复选框的总数。

 <input type="checkbox" value="username1" name="check[0]" id="1" /><br/>
 <input type="checkbox" value="userusername2" name="check[1]" id="1" /><br/>
 <input type="checkbox" value="userusername3" name="check[2]" id="1" /><br/>

I can not change the name of the check boxes as i need to send the values to serverside PHP script as an array. 我无法更改复选框的名称,因为我需要将值作为数组发送到serverside PHP脚本。

Since all other answers are jquery based, I'll offer a pure javascript solution. 由于所有其他答案都是基于jquery的,我将提供一个纯粹的JavaScript解决方案。 Assuming the following form: 假设以下形式:

<form id="myform">
    <input type="checkbox" value="username1" name="check[0]" /><br/>
    <input type="checkbox" value="userusername2" name="check[1]" /><br/>
    <input type="checkbox" value="userusername3" name="check[2]" /><br/>
</form>

You could compute the number of checkbox elements with the following logic: 您可以使用以下逻辑计算复选框元素的数量:

<script type="text/javascript">
var myform = document.getElementById('myform');
var inputTags = myform.getElementsByTagName('input');
var checkboxCount = 0;
for (var i=0, length = inputTags.length; i<length; i++) {
     if (inputTags[i].type == 'checkbox') {
         checkboxCount++;
     }
}
alert(checkboxCount);
</script>

BTW: As others have noted, the id attribute in any HTML tag should be unique within the document. 顺便说一句:正如其他人所说,任何HTML标记中的id属性在文档中应该是唯一的。 I've omitted your id="1" attributes in my sample HTML above. 我在上面的示例HTML中省略了你的id="1"属性。

Update: 更新:

If you simply want to count all checkbox elements on the entire page without using a containing form element, this should work: 如果您只想计算整个页面上的所有复选框元素而不使用包含表单元素,则应该可以:

<script type="text/javascript">
var inputTags = document.getElementsByTagName('input');
var checkboxCount = 0;
for (var i=0, length = inputTags.length; i<length; i++) {
     if (inputTags[i].type == 'checkbox') {
         checkboxCount++;
     }
}
alert(checkboxCount);
</script>

In Plain JavaScript: 在纯JavaScript中:

var myForm = document.forms[nameOrIndex];
var inputs = myForm.getElementsByTagName('input');
var checkboxes = [];
for(var i=0;i<inputs.length;i++){
  if(inputs[i].getAttribute('type').toLowerCase() == 'checkbox'){
    checkboxes.push(inputs[i]);
  }
}
alert(checkboxes.length);

I would go with: 我会选择:

alert(document.querySelectorAll("input[type=checkbox]").length);

If you wanted a particular form you would need to select the form and use that as a base for your call to querySelectorAll instead of document or change the selector to include the form. 如果您需要特定表单,则需要选择表单并将其用作调用querySelectorAll的基础而不是文档或更改选择器以包含表单。

<form id="aForm">
    <input type="checkbox" value="userusername2" name="check[1]" id="1" /><br/>
    <input type="checkbox" value="userusername3" name="check[2]" id="1" /><br/>
</form>
<form id="bForm">
    <input type="checkbox" value="username1" name="check[0]" id="1" /><br/>
    <input type="checkbox" value="userusername2" name="check[1]" id="1" /><br/>
    <input type="checkbox" value="userusername3" name="check[2]" id="1" /><br/>
</form>

Then use: 然后使用:

alert(document.querySelectorAll("#aForm > input[type=checkbox]").length); //shows 2

alert(document.querySelectorAll("#bForm > input[type=checkbox]").length); //shows 3

Note: The Selectors API is only available in newer browsers starting with: Internet Explorer 8, Firefox 3.5, Safari 3.1, Chrome 1, and Opera 10. 注意:Selectors API仅适用于较新的浏览器:Internet Explorer 8,Firefox 3.5,Safari 3.1,Chrome 1和Opera 10。

you could use jquery 你可以使用jquery

var len = $('input:checkbox').length;

alert(len);

WORKING DEMO 工作演示

alert( $("form input[type='checkbox']").length );

将使用jQuery为您提供表单中的所有复选框。

As you tagged your question with php and you seem to use some sort of numbering already for the form fields, you can also just echo that php counter to a javascript variable: 当你标记你的问题php ,你似乎已经使用某种编号的表单字段,您也可以直接echo的是PHP的计数器的JavaScript变量:

<?php
//
?>
<script language="javascript" type="text/javascript">
  var checkbox_counter = <?php echo $your_counter_in_php; ?>
</script>
<?php
//
?>

By the way, in html you can only have one id on a page and it can´t start with a number. 顺便说一句,在html中,你只能在页面上有一个id ,而且它不能以数字开头。

if you have jQuery you could do something like 如果你有jQuery,你可以做类似的事情

alert ($(':checkbox').length ());

If not then you'll have to document.getElementsByTagName ('input') , iterate over the collection you get back and increment a counter every time you encounter one with its type attribute set to checkbox. 如果没有那么你将需要document.getElementsByTagName ('input') ,迭代你得到的集合,并在每次遇到一个其类型属性设置为复选框的计数器时递增一个计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM