全选取消全选网上没看到自己喜欢的。

<!DOCTYPE html>
<html>
  <head> 
  <meta charset="UTF-8" /> 
  <title>jQuery实现CheckBox全选、全不选</title> 
  <script src="js/jquery-1.11.1.js" type="text/javascript"></script> 
  <script type="text/javascript">
        $(function() {
   checkboxAll = {
       selectall:function(a, b) {
           //a表示全选name值,b表示子checkbox的name值
           var $select_all = $("input[name='" + a + "']");
           var $childBox = $("input[name='" + b + "']");
           for (var i = 0; i < $select_all.length; i++) {
               $select_all[i].onclick = function() {
                   var checkTure = $(this).prop("checked");
                   var l = $("input[type='checkbox']").length;
                   for (var j = 0; j <= l; j++) {
                       var childAll = $("input[type='checkbox']").eq(j);
                       if (checkTure) {
                           childAll.prop("checked", true);
                       } else {
                           childAll.prop("checked", false);
                       }
                   }
               };
           }
           for (var i = 0; i < $childBox.length; i++) {
               $childBox[i].onclick = function() {
                   var checkTure = $(this).prop("checked");
                   //判断点击商品选中状态
                   var childlen = $("input[name='" + b + "']:checked").length;
                   //判断商品是否选中个数
                   if (childlen != $childBox.length) {
                       //判断全选下面的商品是否全部选中
                       $(this).prop("checked", false);
                       for (var m = 0; m <= $select_all.length; m++) {
                           //取消全选样式
                           var notSelect = $("input[name='" + a + "']").eq(m);
                           notSelect.prop("checked", false);
                       }
                   } else {
                       //点击商品选中
                       $(this).prop("checked", true);
                       var childlen = $("input[name='" + b + "']:checked").length;
                       //判断商品是否选中个数
                       if (childlen != $childBox.length) {
                           //判断商品是否全部选中
                           for (var m = 0; m <= $select_all.length; m++) {
                               var notSelect = $("input[name='" + a + "']").eq(m);
                               notSelect.prop("checked", false);
                           }
                       } else {
                           for (var m = 0; m <= $select_all.length; m++) {
                               var notSelect = $("input[name='" + a + "']").eq(m);
                               notSelect.prop("checked", true);
                           }
                       }
                   }
                   if (checkTure) {
                       //控制本身是否选中
                       $(this).prop("checked", true);
                   } else {
                       $(this).prop("checked", false);
                   }
               };
           }
       }
   };
   checkboxAll.selectall("all", "childBox");
});
    </script> 
 </head> 
  <body> 
  <div style="width: 100%;"> 
  <div style="width: 300px; margin: 100px auto; border: 1px solid #ccc; padding: 20px;"> 
   <input id="checkAll" name="all" type="checkbox" />全选 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childBox" type="checkbox" />项1 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childBox" type="checkbox" />项2 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childBox" type="checkbox" />项3 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childBox" type="checkbox" />项4 
   <br /> 
   <input id="checkAll0" name="all" type="checkbox" />全选 
  </div> 
  </div>  
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值