一个页面多个全选,根据父节点来实现

来源:互联网 发布:公寓租房软件 编辑:程序博客网 时间:2024/04/30 21:20
<!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($) {
   $.fn.checkboxAll = function(a, b, c) {
           //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 = $(this).parent(c).find("input[type='checkbox']").length;
                   alert(l)
                   for (var j = 0; j <= l; j++) {
                       var childAll = $(this).parent(c).find("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);
                   }
               };
           }
       };
})(jQuery);
$(function(){
$(".all").checkboxAll("all", "childBox", ".a");
   $(".test").checkboxAll("test", "childTest", ".b");
});
    </script> 
 </head> 
  <body> 
  <div style="width: 100%;"> 
  <div class="a" style="width: 300px; margin: 100px auto; border: 1px solid #ccc; padding: 20px;"> 
   <input name="all" class="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 name="all" class="all" type="checkbox" />全选 
  </div> 
  <div class="b" style="width: 300px; margin: 100px auto; border: 1px solid #ccc; padding: 20px;"> 
   <input id="checkAll" name="test" class="test" type="checkbox" />全选 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childTest" type="checkbox" />项1 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childTest" type="checkbox" />项2 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childTest" type="checkbox" />项3 
   <br /> &nbsp;&nbsp;&nbsp;&nbsp;
   <input name="childTest" type="checkbox" />项4 
   <br /> 
   <input name="test" class="test" type="checkbox" />全选 
  </div> 
  </div>  
  </body>
</html>
0 0
原创粉丝点击