方式一
① 點(diǎn)擊全選按鈕實(shí)現(xiàn)全選與全不選
var len = $("input[name='box']").length; //復(fù)選框個數(shù)
$("#allBox").click(function () {
if($("#allBox").is(':checked')){
$("input[name='box']").prop("checked","true");
}else {
var checked = $("input[name='box']:checked").length; //復(fù)選框被選中的個數(shù)
if (len==checked){
$("input[name='box']").removeAttr("checked");
}
}
});
② 如果有一個未選中則取消全選按鈕的選中狀態(tài)/全都選擇設(shè)置全選按鈕為選中狀態(tài)
var len = $("input[name='box']").length; //復(fù)選框個數(shù)
$("input[name='box']").click(function () {
//復(fù)選框被選中的個數(shù)
var checked = $("input[name='box']:checked").length;
if (len==checked){
$("#allBox").prop("checked","true");
}else {
$("#allBox").removeAttr("checked");
}
});
代碼中allBox 代表全選按鈕的id屬性,box代表復(fù)選框的name屬性
方式二
① 點(diǎn)擊全選按鈕實(shí)現(xiàn)全選與全不選
$("#allSelBox").click(function () { //全選框按鈕點(diǎn)擊事件
var flg = this.checked;
$("#userData :checkbox").each(function () {
this.checked = flg;
});
});
② 如果有一個未選中則取消全選按鈕的選中狀態(tài)/全都選擇設(shè)置全選按鈕為選中狀態(tài)
$(document).on("click",".check_item",function(){ //復(fù)選框點(diǎn)擊事件
console.log('dsgsdfhg')
//判斷當(dāng)前選擇中的元素是否等于總復(fù)選框個數(shù)
var flag = $(".check_item:checked").length==$(".check_item").length;
$("#allSelBox").prop("checked",flag);
});
代碼中allSelBox 代表全選按鈕的id屬性毁渗,check_item代表復(fù)選框的class屬性