全选:<input type="checkbox" id="checkAll"> <br>
选项:<input type="checkbox" class="checkOne">A
<input type="checkbox" class="checkOne">B
<input type="checkbox" class="checkOne">C
<input type="checkbox" class="checkOne">D
<script>
var chkAll = document.querySelector('#checkAll');
var chkOne = document.querySelectorAll('.checkOne');
console.log(chkAll,chkOne);
chkAll.onclick = function(){
for(var i = 0 ; i < chkOne.length ; i++){
chkOne[i].checked = this.checked
}
}
for(var i = 0 ; i < chkOne.length ; i++){
chkOne[i].onclick = function(){
for(var j = 0 ; j < chkOne.length ;j++){
if(!chkOne[j].checked){
break
}
}
chkAll.checked = (j == chkOne.length);
}
}
</script>
js简易全选和反选
最新推荐文章于 2024-09-03 17:02:58 发布