首页 > 代码库 > jq变态全选vs原生变态全选

jq变态全选vs原生变态全选

<script>
$(function(){
var num=0;
$("#btn").on(‘click‘,function(){
if(this.checked){
$("#box").children().attr(‘checked‘,true);
num=$("#box").children().length;//赋个值
}else{
$("#box").children().attr(‘checked‘,false);
num=0;//赋个值
}
});
$("#box").children().on(‘click‘,function(){
if(this.checked){
num++;
}else{
num--;
}
if(num==$("#box").children().length){
$("#btn").attr(‘checked‘,true);
}else{
$("#btn").attr(‘checked‘,false);
}
});
});
</script>

=======

<script>
window.onload = function(){
var aInput = document.getElementsByTagName(‘input‘);
//上面的全选按钮:
aInput[0].onclick = function(){
if(this.checked == true){
for(var i = 0; i < aInput.length; i++){
aInput[i].checked = true;
}
num = aInput.length - 1;
}else{
for(var i =0; i < aInput.length; i++){
aInput[i].checked = false;
}
num = 0;
}
}
var num = 0;
for(var i = 1; i < aInput.length; i++){
aInput[i].onclick = function(){
if(this.checked == true){
num++;
}else{
num--;
}
if(num == aInput.length - 1){
aInput[0].checked = true;
}else{
aInput[0].checked = false;
}
}
}
}
</script>

jq变态全选vs原生变态全选