使用 JavaScript 检查所有复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9493089/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:51:20  来源:igfitidea点击:

Check all checkboxes with JavaScript

javascriptcheckbox

提问by Chris Edwards

I want to have the first checkbox that allows me to check or uncheck all of the other boxes. Here is the code I am using:

我想要第一个复选框,允许我选中或取消选中所有其他框。这是我正在使用的代码:

<html>
<head>
<script language="JavaScript">
function toggle(source) {
  checkboxes = document.getElementsById('checkall');
   for(var i in checkboxes)
     checkboxes[i].checked = source.checked;
 }
 </script> 
 </head>
 <body>
 <input type='checkbox' onClick='toggle(this)' /><br />
 <input type='checkbox' id='checkall' name='orders[0][order_id]' value='16885' /><br />
 <input type='checkbox' id='checkall' name='orders[1][order_id]' value='17006' /><br />
 <input type='checkbox' id='checkall' name='orders[2][order_id]' value='17006' /><br />
 <input type='checkbox' id='checkall' name='orders[3][order_id]' value='17007' /><br />
 <input type='checkbox' id='checkall' name='orders[4][order_id]' value='17011' /><br />
 </body>
 </html>

回答by Umbrella

This has worked for me.

这对我有用。

function toggle(oInput) {
    var aInputs = document.getElementsByTagName('input');
    for (var i=0;i<aInputs.length;i++) {
        if (aInputs[i] != oInput) {
            aInputs[i].checked = oInput.checked;
        }
    }
}

Though, if you want to limit this to only certain checkboxes, add a classname to them, and to the master checkbox

但是,如果您想将其限制为仅某些复选框,请为它们添加一个类名,并添加到主复选框

<html>
<head>
<script type="text/javascript">
    function toggle(source) {
        var aInputs = document.getElementsByTagName('input');
        for (var i=0;i<aInputs.length;i++) {
            if (aInputs[i] != source && aInputs[i].className == source.className) {
                aInputs[i].checked = source.checked;
            }
        }
    }
 </script> 
 </head>
 <body>
 <input type='checkbox' class='checkall' onClick='toggle(this)' /><br />
 <input type='checkbox' class='checkall' name='orders[0][order_id]' value='16885' /><br />
 <input type='checkbox' class='checkall' name='orders[1][order_id]' value='17006' /><br />
 <input type='checkbox' class='checkall' name='orders[2][order_id]' value='17006' /><br />
 <input type='checkbox' class='checkall' name='orders[3][order_id]' value='17007' /><br />
 <input type='checkbox' class='checkall' name='orders[4][order_id]' value='17011' /><br />
 </body>
 </html>

回答by ruin3936

Try this...

试试这个...

<form id="form_">
    <input type='checkbox' name='item1' value='16885' />
    <input type='checkbox' name='item1' value='17006' />
    <input type='checkbox' name='item1' value='17006' />
    <input type='checkbox' name='item1' value='17007' />
    <input type='checkbox' name='item1' value='17011' />
    <a href="javascript:;" id="select_all1"><br/>Select All</a>
    <a href="javascript:;" id="clear_all1"><br/>Clear All</a>
    <button id="btnRemove1"><br/>Remove</button>
</form>

<script>
$("#select_all1").click(function() {
    var item = document.getElementsByName("item1");
    for (var i=0; i < item.length; i++) {
        item[i].setAttribute("checked");
    }
});
$("#clear_all1").click(function() {
    var item = document.getElementsByName("item1");
    for (var i=0; i < item.length; i++) {
        if(item[i].checked) {
            item[i].removeAttribute("checked");
        } else {
            alert("Nothing to clear.");
            return false;
        }
    }
});
$("#btnRemove1").click(function() {
    var items = $('.item1').is(':checked');
    if(items) {
            window.location = "/contents?"+ $("form#form_").serialize();
    }
    else {
        alert("Nothing to remove.");
        return false;
    }
});
</script>

回答by Michael Berkowski

The problem is you are using the same id for all the checkbox groups. An id must be unique to a page. Instead you may use the checkbox name. Since the names have []with varying values, you can use indexOfto examine just the first part.

问题是您对所有复选框组使用相同的 ID。一个 id 对一个页面来说必须是唯一的。相反,您可以使用复选框名称。由于名称具有[]不同的值,因此您可以indexOf只检查第一部分。

<html>
<head>
<script language="JavaScript">
function toggle(source) {
  // Get all input elements
  var inputs = document.getElementsByTagName('input'); 
   // Loop over inputs to find the checkboxes whose name starts with `orders`
   for(var i =0; i<inputs.length; i++) {
     if (inputs[i].type == 'checkbox' && inputs[i].name.indexOf('orders') === 0) { 
       inputs[i].checked = source.checked;
     }
   }
 }
 </script> 
 </head>
 <body>
 <input type='checkbox' onClick='toggle(this)' /><br />
 <input type='checkbox' id='checkall' name='orders[0][order_id]' value='16885' /><br />
 <input type='checkbox' id='checkall' name='orders[1][order_id]' value='17006' /><br />
 <input type='checkbox' id='checkall' name='orders[2][order_id]' value='17006' /><br />
 <input type='checkbox' id='checkall' name='orders[3][order_id]' value='17007' /><br />
 <input type='checkbox' id='checkall' name='orders[4][order_id]' value='17011' /><br />
 </body>
 </html>

回答by R J

It's better to use the querySelectorAll. here is the example.

最好使用querySelectorAll. 这是示例。

The Html with checkboxes

Html 与 checkboxes

 <input type="button" value="Select All" onclick="selectAll()" id="TestAll" />

 <input type='checkbox' id='checkAll' name='Test1' />
 <input type='checkbox' id='checkall' name='Test2' />
 <input type='checkbox' id='checkAll' name='Test3' />
 <input type='checkbox' id='checkAll' name='Test4' />
 <input type='checkbox' id='checkAll' name='Test5' />

Here is the javascript for this

这是用于此的 javascript

function selectAll() {
    var checkboxes = document.querySelectorAll('input[type="checkbox"]');
     for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].type == 'checkbox')
        checkboxes[i].checked = true;
     }
 }

回答by obenjiro

You can simply wrap all checkboxes that you nead itterate throu into the DIV and access to it's childNodesand use getElementByIdto access that DIV

您可以简单地将所有您想要通过的复选框包装到 DIV 中并访问它childNodes并用于getElementById访问该 DIV

<head> 
<script language="JavaScript"> 
function toggle(source) { 
  //use getElementById to access to DOM objects by ID
  var checkboxes = document.getElementById('checkall').childNodes; 
  var source = document.getElementById('source');
  //now just itterate throu all checkboxess that is in the 'checkall' DIV
  for(var i in checkboxes) {
     checkboxes[i].checked = source.checked; 
  }
} 
</script>  
</head> 
<body> 
<!--Give an ID to the source checkbox so we could access it from Javascript-->
<input id="source" type='checkbox' onClick='toggle(this)' /><br /> 

<!--Just wrap all checkboxes that you nead to itterate into the DIV-->
<div id="checkall">
<input type='checkbox' name='orders[0][order_id]' value='16885' /><br /> 
<input type='checkbox' name='orders[1][order_id]' value='17006' /><br /> 
<input type='checkbox' name='orders[2][order_id]' value='17006' /><br /> 
<input type='checkbox' name='orders[3][order_id]' value='17007' /><br /> 
<input type='checkbox' name='orders[4][order_id]' value='17011' /><br /> 
</div> 

</body> 
</html> 

回答by Ry-

IDs are supposed to be unique - neverhave more than one HTML element with the same id, it's invalid. That's also why you have a problem - there is no such method as document.getElementsById, only document.getElementById. Instead, you could use classes. Here's how to solve your problem in pure JavaScript:

ID 应该是唯一的 -永远不会有多个具有相同 的 HTML 元素id,它是无效的。这也是您遇到问题的原因 - 没有这样的方法document.getElementsById,只有document.getElementById. 相反,您可以使用类。以下是如何在纯 JavaScript 中解决您的问题:

function toggle(source) {
    var inputs = document.getElementsByTagName('input');
    var i, input;

    for(i = 0; input = inputs[i]; i++) {
        if((' ' + input.className + ' ').indexOf(' checkall ') > -1) {
            input.checked = source.checked;
        }
    }
}

And change all your id="checkall"s to class="checkall".

并将您id="checkall"的所有s更改为class="checkall".



Or you could use jQuery. It's great and does all things ;)

或者你可以使用 jQuery。它很棒,可以做所有事情;)