$(function(){
//全选
$("#ckbAll").click(function(){
//获取到全选状态
var status=$(this).prop("checked");
//遍历将所有的checkbox状态改变成ckbAll的状态
$("[name='ckb']").each(function(index,item){
$(item).prop("checked",status);
})
});
//反选,为所有的ckb注册点击事件
$("[name='ckb']").click(function(){
//初始化状态
var status=true;
//当点击一个ckb、时,遍历所有的ckb,有一个未被选中,ckbAll为false
$("[name='ckb']").each(function(index,item){
if(!$(item).prop("checked")){
status=false;
return;
}
});
//替换ckbAll的选中状态
$("#ckbAll").prop("checked",status);
})
})
<body>
<input type="checkbox" id="ckbAll"/><br/>
<input type="checkbox" name="ckb"/><br/>
<input type="checkbox" name="ckb"/><br/>
<input type="checkbox" name="ckb"/><br/>
<input type="checkbox" name="ckb"/><br/>
<input type="checkbox" name="ckb"/><br/>
</body>