每页展示固定条数据的单选本页选择和全选的逻辑及js代码实现

本文详细阐述了实现每页数据全选和单选功能的逻辑,包括全选按钮勾选时如何影响本页及单选项,本页选项变化时如何影响全选和单选,以及单选项的勾选状态对全选和本页的影响。同时,提到了两个关键变量:全选标记`selectAll`和已选择项`needContactsIds`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

逻辑:

1.全选:勾选时,本页勾选,单选全部勾选;取消勾选时,本页取消勾选,单选全部取消勾选

2.本页:勾选时,单选全部勾选,若勾选总数等于查询结果总数,全选也勾选;取消勾选时,本页全部取消勾选,全选取消勾选

3.单选:勾选时,判断已勾选条数,若等于本页总数,则本页勾选,若等于查询结果总数,则全选勾选;取消勾选时,本页取消勾选,全选取消勾选


变量:

1.selecteAll    全选标记

2.needContactsIds    已选择项


代码:

function bindCheckbox(){
	//翻页清除本页选择
	$('#selectAllByPage').removeAttr("checked");
	
	//列表总数
	totalRecords = Number($('#totalRecords').html());
	
	//翻页之后的全选
	if(selectAll){
		$('#selectAllByPage').prop("checked",true);
		$('#contactsView input').prop("checked",true);
		needContactsIds = ",";
	}	
	
	

	//点击全选
	$('#selectAll').bind("click",function(){
		if($(this).is(":checked")){//选中
			selectAll = true;//全选标记
			$('#selectAllByPage').prop("checked",true);
			$('#contactsView input').prop("checked",true);
		}
		else{
			selectAll = false;
			$('#selectAllByPage').removeAttr("checked");
			$('#contactsView input').removeAttr("checked");
			
		}
		needContactsIds = ",";// 已选文件无记录
	});
	
	//点击分页全选
	$('#selectAllByPage').bind("click",function(){				
		if($(this).is(":checked")){//选中
			$.each($('#contactsView input'),function(i,v){					
				$(v).prop("checked",true);
				var key = $(v).attr("id");
				if(needContactsIds.indexOf(','+key+',') < 0){
					needContactsIds = needContactsIds + key + ',';
				}					
			});
			var contactsArray = needContactsIds.split(",");
			if((contactsArray.length-2) == totalRecords){
				selectAll = true;					
				$("#selectAll").prop("checked",true);
				needContactsIds = ",";
			}
		}else{
			$("#selectAll").removeAttr("checked");
			selectAll = false;
			$.each($('#contactsView input'),function(i,v){
				$(v).removeAttr("checked");
				var key = $(v).attr("id");
				if(needContactsIds.indexOf(','+ key +',')>-1){
					needContactsIds = needContactsIds.replace(','+key+',',',');
				}						
			});
		}	
	});
	//点击普通checkbox
	$('#contactsView input').bind("click",function(){
		if($(this).is(":checked")){
			var key = $(this).attr("id");			
			if(needContactsIds.indexOf(','+key+',') < 0){
				needContactsIds = needContactsIds + key + ',';
			}
			var contactsArray = needContactsIds.split(",");
			//每页显示的数量
			if((contactsArray.length-2) == $('#contactsView input').length){				
				$("#selectAllByPage").prop("checked",true);
			}
			if((contactsArray.length-2) == totalRecords){
				selectAll = true;					
				$("#selectAll").prop("checked",true);
				$("#selectAllByPage").prop("checked",true);
				needContactsIds = ",";
			}
		}else{
			var key = $(this).attr("id");			
			if(needContactsIds.indexOf(','+key+',') > -1){
				needContactsIds = needContactsIds.replace(','+key+',',',');
			}
			//其它已选中需添加到变量,避免全选再取消单选needContactsIds无内容
			$.each($('#contactsView input'),function(i,v){
				if($(this).is(":checked")){
					var key = $(this).attr("id");			
					if(needContactsIds.indexOf(','+key+',') < 0){
						needContactsIds = needContactsIds + key + ',';
					}
				}
			});
			$("#selectAll").removeAttr("checked");
			$("#selectAllByPage").removeAttr("checked");
			selectAll = false;
		}	
	});
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值