[英]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>
我在while循環中運行了5次的復選框。 我在循環上方有一個復選框。 單擊上面的復選框時,我想選中循環內的所有復選框。
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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.