JQuery应用案例--全选与反选

本文介绍使用JQuery实现表格中的全选与反选功能。通过点击全选按钮可以控制所有子项的选择状态,而点击反选按钮则实现已选项目的取消选择与未选项目的选中。文中提供了一个具体的实现示例。

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

用JQuery实现全选与反选功能。不过下面代码中实现的反选功能对全选有影响,尚未找到解决办法,如下图:



代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="../js/jquery-1.8.2.js"></script>
  </head>
  <body>
	<table border="1" align="center">
		<tr>
			<th>状态</th>
			<th>用户名</th>
		</tr>
		<tr>
			<td><input type="checkbox"/></td>
			<td>赵</td>
		</tr>
		<tr>
			<td><input type="checkbox"/></td>
			<td>钱</td>
		</tr>		
		<tr>
			<td><input type="checkbox"/></td>
			<td>孙</td>
		</tr>	
		<tr>
			<td><input type="checkbox"/></td>
			<td>李</td>
		</tr>
		<tr>
			<td><input type="checkbox"/></td>
			<td>周</td>
		</tr>	
		<tr>
			<td>
				<input type="checkbox"/>全选
			</td>
			<td><input type="button" value="全反选"/></td>
		</tr>		
	</table>
	
	<script type="text/javascript">
		
		$(":checkbox:last").click(function(){
			//全选
			if($(":checkbox:last").is(":checked")){
				$(":checkbox:not(:last)").attr("checked","checked");
			}else{
				$(":checkbox:not(:last)").removeAttr("checked","checked");
			}
		});
		
		//反选
		
		$(":button").click(function(){
			//方式一
			$(":checkbox").not(":checkbox:last").each(function(){
				if($(this).is(":checked")){
					$(this)	.removeAttr("checked","checked");
				}else{
					$(this).attr("checked","checked");
				}
			});	
		
			/*
			//方式二
			//将已经选中的复选框失效
			$(":checkbox:checked").attr("disabled","disabled");
			//将剩下的未选中的复选框选中
			$(":checkbox:not(:checked)").attr("checked","checked");
			//将失效的复选框生效
			$(":checkbox:disabled").removeAttr("disabled","disabled").removeAttr("checked","checked");
			*/	
			
		});
		
	</script>
  </body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君醉酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值