jquery 实现checkbox全选、取消全选功能

来源:互联网 发布:阿里云做socket服务器 编辑:程序博客网 时间:2024/05/01 12:48
<script>

$(document).ready(function(){
 $("#cAll").click(function(){
 var flag = $(this).attr("checked"); //读取对象的状态(是否选中)
// alert(flag);
 if(!flag)
 $("[name = c]:checkbox").attr("checked",false);
 else
 $("[name = c]:checkbox").attr("checked",true);
 });

//全选后,取消某个选中后,全选的checkbox取消选中状态
$("[name=c]:checkbox").click(function(){
 var flag = true;
 $("[name=c]:checkbox").each(function(){
 if(!this.checked)
  {
  flag = false;
  }
 });
   $("#cAll").attr("checked",flag);
 });
});
  
</script>

<style>
#box{width:600px; margin:100px auto;}
#box input{margin:0 5px; line-height:12px;}
</style>


<div id="box">
 <input type="checkbox" id="cAll">全选</input>
 <input type="checkbox" id="c1" name="c">香蕉</input>
 <input type="checkbox" id="c2" name="c">橘子</input>
 <input type="checkbox" id="c3" name="c">桃子</input>
 <input type="checkbox" id="c4" name="c">梨子</input>
 <input type="checkbox" id="c5" name="c">苹果</input>
 <input type="checkbox" id="c6" name="c">桂圆</input>
 <input type="checkbox" id="c7" name="c">西瓜</input>
</div>
0 0
原创粉丝点击