Check all checkboxes with JavaScript

不问归期 提交于 2021-02-08 11:11:44

问题


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>

回答1:


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>



回答2:


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 indexOf to examine just the first part.

<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>



回答3:


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>



回答4:


IDs are supposed to be unique - never have 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:

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".


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




回答5:


You can simply wrap all checkboxes that you nead itterate throu into the DIV and access to it's childNodes and use getElementById to access that 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> 



回答6:


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

The Html with 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

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;
     }
 }


来源:https://stackoverflow.com/questions/9493089/check-all-checkboxes-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!