jquery ajax批量删除

来源:互联网 发布:虚拟摄像头软件 编辑:程序博客网 时间:2024/06/06 07:11
js页面jquery代码: 
复制代码代码如下:

// JavaScript Document 
$(document).ready(function() { 
// 全选 
$("#allChk").click(function() { 
$("input[name='subChk']").prop("checked",this.checked); 
}); 
// 单选 
var subChk = $("input[name='subChk']") 
subChk.click(function() { 
$("#allChk").prop("checked", subChk.length == subChk.filter(":checked").length ? true:false); 
}); 
/* 批量删除 */ 
$("#del_model").click(function() { 
// 判断是否至少选择一项 
var checkedNum = $("input[name='subChk']:checked").length; 
if(checkedNum == 0) { 
alert("请选择至少一项!"); 
return; 

// 批量选择 
if(confirm("确定要删除所选项目?")) { 
var checkedList = new Array(); 
$("input[name='subChk']:checked").each(function() { 
checkedList.push($(this).val()); 
}); 
$.ajax({ 
type: "POST", 
url: "deletemore", 
data: {'delitems':checkedList.toString()}, 
 success: function(result) { 
$("[name ='subChk']:checkbox").attr("checked", false); 
window.location.reload(); 

}); 

}); 
}); 
页面元素: 
<a href="#" id="del_model"><span>删除用户</span> 
<th class="tal"><input type="checkbox" id="allChk"/>全选</th> 
<td><input type="checkbox" name="subChk" value="${user.id}"/></td> 

java后台代码: 
复制代码代码如下:

@RequestMapping(value = "/deletemore", method = RequestMethod.POST) 
public String deleteMore(HttpServletRequest request, HttpServletResponse response) { 
String items = request.getParameter("delitems"); 
String[] item = items.split(","); 
for (int i = 0; i < item.length; i++) { 
userService.delete(Integer.parseInt(item[i])); 

return "redirect:list"; 
0 0
原创粉丝点击