简体   繁体   中英

Check all check box with a check box click

 <script> function chkall() { var checkbox = document.getElementById('checkbox'); var checkbox2 = document.getElementById('checkbox2'); if(checkbox.checked==true) { checkbox2.checked=true; } } </script> 
 <table width="10%" border="0"> <tr> <th scope="col"><input type="checkbox" onclick="chkall()" name="checkbox" id="checkbox" /></th> </tr> <?php $i=0; while($i<5) { ?> <tr> <th scope="col"><input type="checkbox" name="checkbox2" id="checkbox2" /></th> </tr> <?php $i++; } ?> </table> 

I have check box inside while loop which is running 5 times. I have a check box above the loop. I want to check all check boxes inside the loop when i clicked on the above check box.

function checkAll(ele) {
 var checkboxes = document.getElementsByTagName('input');
 if (ele.checked) {
     for (var i = 0; i < checkboxes.length; i++) {
         if (checkboxes[i].type == 'checkbox') {
             checkboxes[i].checked = true;
         }
     }
 } else {
     for (var i = 0; i < checkboxes.length; i++) {
         console.log(i)
         if (checkboxes[i].type == 'checkbox') {
             checkboxes[i].checked = false;
         }
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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