实现一个功能:点击全选框,所有复选框都选中或全不选中;只要有一个复选框不选中,全选框也不选中。
<script>
$(document).ready(function(){
//全选框点击事件
$("input[name=checkedAll]").click(function(){
if($(this).prop("checked")){
$("input[name=goodsChecked]").prop("checked",true);
}else{
$("input[name=goodsChecked]").prop("checked",false);
}
});
//复选框与全选框联动点击事件
$("input[name=goodsChecked]").click(function(){
var arr = $("input[name=goodsChecked]:checked");
var length = $("input[name=goodsChecked]").length;
if(arr.length == length){
$("input[name=checkedAll]").prop("checked",true);
}else{
$("input[name=checkedAll]").prop("checked",false);
}
});
});
</script>
<body>
//复选框们
<input type="checkbox" name="goodsChecked">
<input type="checkbox" name="goodsChecked">
<input type="checkbox" name="goodsChecked">
//全选框
<input type="checkbox" name="checkAll">
</body>