Home  >  Article  >  Web Front-end  >  jquery one-click method to control checkbox selection, inverse selection, and deselection

jquery one-click method to control checkbox selection, inverse selection, and deselection

一个新手
一个新手Original
2017-10-17 09:44:001741browse

The jquery attr() method may cause problems when obtaining the checked tag, so the prop() method is used.

Hml's checkbox does not add a name, only p nesting is used.

If there is a better method, please give it!!

//全选$('#allChecked').change(function(){
     $('#box').children(':checkbox').prop('checked',$(this).is(':checked')?true:false);
});
//反选$('#invertChecked').change(function(){  
        if($(this).is(':checked')){
     $('#box').children(':checkbox').each(function(){
       $(this).prop('checked',$(this).is(':checked')?false:true);
     });
  }
});
//一键控制全选、反选、全不选
$('#orChecked').change(function(){
  if($(this).is(':checked')){
     var box = $('#box').children(':checkbox');
     if(box.length==box.filter(':not(:checked)').length){    // 复选框长度和没选中的个数一样 -> 全选 , .not(':checked').length 也可以。
     $('#box').children(':checkbox').prop('checked',true);
  }else{     // 如果有选中个数,-> 反选 
     $('#box').children(':checkbox').each(function(){     
        $(this).prop('checked',$(this).is(':checked')?false:true);
     });
  }else{
      $('#box').children(':checkbox').prop('checked',false);    // 如控制键取消选中,剩余的checkbox也取消选中
  }
    
});
西瓜 芒果 山竹 草莓 火龙果

全选 反选 全选/反选/全不选

If there is a better method, please give it!!

The above is the detailed content of jquery one-click method to control checkbox selection, inverse selection, and deselection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn