php+jQuery全选 反选

本文介绍了一个使用Ajax实现的批量删除功能。通过全选、反选按钮和删除按钮,可以轻松地选择并删除多个项目。具体实现包括了前端的jQuery交互逻辑及后端的PHP处理过程。

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

index.php

<html> 
<head>
	<title>ajax批删除</title>
	<script language='JavaScript' src='jquery.js'></script>
</head>
<script type="text/javascript">

$(document).ready(function() { 
	var checkAll = $("input[name='checkAll']") ;
	// 全选 
	$("#selAll").click(function() { 
		$("input[name='checkAll']").attr("checked",this.checked); 
	}); 
	// 反选
	$("#inverse").click(function() { 
		$("input[name='checkAll']").each(function() {
			$(this).attr("checked",!this.checked);
		}); 
		$("#selAll").attr("checked", checkAll.length == checkAll.filter(":checked").length ? true:false); 
	});
	// 单选 
	checkAll.click(function() { 
		$("#selAll").attr("checked", checkAll.length == checkAll.filter(":checked").length ? true:false); 
	}); 
	
	$("#del").click(function() { 
		
		var checkedNum = $("input[name='checkAll']:checked").length; 
		if(checkedNum == 0) { 
			alert("请选择至少一项!"); 
			return; 
		} 
		// 批量选择 
		if(confirm("确定要删除所选项目?")) { 
			var checkedList = new Array(); 
			$("input[name='checkAll']:checked").each(function() { 
				checkedList.push($(this).val()); 
			});
			$.ajax({ 
				type: "POST", 
				url: "receive.php", 
				data: {'allValues':checkedList.toString()}, 
				 success: function(result) { 
					alert(result);
				} 
			}); 
		} 
	}); 
}); 
</script> 

<body> 
<center> 
<input type="checkbox" id="selAll"/>全选 
<input type="checkbox" id="inverse"/>反选
<input type="button" id="del" value="删除" />
<div id="allcheck"> 
<input type="checkbox" name="checkAll" id="checkAll" onclick="setSelectAll();" value="1" />1<br> 
<input type="checkbox" name="checkAll" id="checkAll" onclick="setSelectAll();" value="2"/>2<br> 
<input type="checkbox" name="checkAll" id="checkAll" onclick="setSelectAll();" value="3"/>3<br> 
<input type="checkbox" name="checkAll" id="checkAll" onclick="setSelectAll();" value="4"/>4<br> 
<input type="checkbox" name="checkAll" id="checkAll" onclick="setSelectAll();" value="5"/>5<br> 
<input type="checkbox" name="checkAll" id="checkAll" onclick="setSelectAll();" value="6"/>6<br> 
</div>	
</center> 
</body> 
</html> 
receive.php

<?php
	$allValues = $_POST['allValues'];
	echo json_encode($allValues);
?>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值