当前位置:编程学习 > JS >>

js 完成checkbox 全选与复选[实例]

全选 :       <input type="checkbox"  id="checkAll" /> 
        1:<input type="checkbox"  name="subBox" /> 
        2:<input type="checkbox"  name="subBox" /> 
        3:<input type="checkbox"  name="subBox" /> 
        4:<input type="checkbox"  name="subBox" /> 

[javascript]
$(function(){ 
    $('#checkAll').click(function(){ 
        $(this).each(function(){ 
              var allFlag = $(this).attr('checked'); 
               $('[name=subBox]:checkbox').attr('checked',allFlag); 
         }); 
    }); 
      $('[name=subBox]:checkbox').click(function(){ 
             var flag = true; 
             var allSelect = true; 
           $('[name=subBox]:checkbox').each(function(index){ 
                    flag = $(this).attr('checked'); 
                    if(!flag){ 
                            allSelect  = false; 
                           $('#checkAll').attr('checked',flag); 
                    } 
              }); 
            if(allSelect){ 
                   $('#checkAll').attr('checked',flag); 
             } 
      }); 
}); 


摘自 wwwyuanliang10000的专栏
补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,