现在的位置: 首页 > 综合 > 正文

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

2013年01月26日 ⁄ 综合 ⁄ 共 630字 ⁄ 字号 评论关闭

全选 :       <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" />

$(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);
             }
      });
});

抱歉!评论已关闭.