checkbox单选 全选实例

单选框就是改变checked属性的值 当checked值为false 点击后值改为true 反之一样 当单选框都选中的时候就是通过判断checked为true的个数跟checkbox的长度是否一致 当checked为true的个数跟checkbox的长度一致的时候就会将全选框checked值改为true 否则为false 即

var count = $("input[name='id']:checkbox:checked").length;
			if (count == $("input[name='id']:checkbox").length) {
				$("input[name=allselect]:checkbox").prop("checked", true);
			}

完整示例

	str += '<td  style="text-align:right; padding-right:0px;"><input type="checkbox" name="id" value='+ datalist[i].id+ ' onclick="userCheck(this)" ></td><td style="width:5%;texalign:left;">'+ datalist[i].id+ '</td>
str += '<div style="width:180px; height:25px;margin-top:10px;"><div style="width:49px; float: left;clear:right;"><input style="float: right; margin-top:10px;" type="checkbox" id="allselect"  name="allselect" onclick="selectedAll()" ></div><div id="delselect" name="delselect" style="float:left; margin-left:10px; height:35px;line-height:35px; width:90px; border-radius:5px; border:1px; border-style:solid; border-color: #444444; color:#cccccc"  onclick="delSelect()" > <img src="img/yhgl_delete_normal.png"  style="vertical-align:middle; margin-right:5px; margin-left:20px; ">删除</div></div>';
							
//单选
	function userCheck(ths) {
		if (ths.checked == false) {
			$("input[name=allselect]:checkbox").prop('checked', false);
		} else {
			var count = $("input[name='id']:checkbox:checked").length;
			if (count == $("input[name='id']:checkbox").length) {
				$("input[name=allselect]:checkbox").prop("checked", true);
			}
		}
	}

	//多选
	function selectedAll() {
		var select = $("input[name='allselect']").is(':checked');
		if (select) {
			$("input[type='checkbox']").prop("checked", true);
		} else {
			$("input[type='checkbox']").prop("checked", false);
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值