jquery的全选和多选操作
阅读原文时间:2023年09月19日阅读:1

//全选产品
$('#checkAll').click(function() {
$(this).prop('checked',!$(this).prop('checked'));
if($(this).prop('checked')) {
$(this).prop('checked',false);
$('#getProduct input').each(function(index,item) {
$(item).prop('checked',false)
})
}else {
$(this).prop('checked',true);
$('#getProduct input').each(function(index,item) {
$(item).prop('checked',true)
});
};
});

//二分查找
function binarySearch(arr,target) {
var l = 0,r = arr.length-1;
while(l <= r) {
var mid = (l+r)/2;
if(arr[mid] == target) {
return mid
}else if(arr[mid] < target) {
l = mid+1
}else {
r = mid-1
}
};
return -1;
}
//多选产品
$('#getProduct input').each(function(index,item) {
$(item).click(function() {
var count = 0;
if (!$(this).prop("checked")) {
$(this).prop("checked", false);
} else {
$(this).prop("checked", true);
};
$('#getProduct input').each(function(index,item) {
if($(item).prop('checked')) {
count ++;
}else {
count --;
};
});
if(count == $('#getProduct input').length) {
$('#checkAll').prop('checked',true)
}else {
$('#checkAll').prop('checked',false)
};
})
})

其中#checkAll是全选按钮的id,#getProduct input是tbody中的所有类型为checkbox的input。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器