教程集 www.jiaochengji.com
教程集 >  jQuery  >  jquery 教程  >  正文 jquery批量删除的实现思路与代码

jquery批量删除的实现思路与代码

发布时间:2015-10-14   编辑:jiaochengji.com
介绍一个Jquery实现批量删除的思路与代码,通过回调函数,把选中的checkbox去掉。有需要的朋友,可以参考下。

说明:回调函数,在请求完成后需要进行的操作:此处是把选中的checkbox去掉。
1、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(); } }); } }); });

2、html页面
 

复制代码 代码示例:
<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>

3、回调函数,在请求完成后需要进行的操作:
此处把选中的checkbox去掉(用到了freemarker的list循环,去掉是数据后checkbox序号变化,还有有相应未知的checkbox被选中,需要去掉)。
 

success: function(result) { $("[name = 'items']:checkbox").attr("checked", false); window.location.reload(); }

4、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";
}

您可能感兴趣的文章:
Jquery post传递数组方法实现思路及代码
jquery批量删除的实现思路与代码
PHP实例:批量删除文件夹及文件夹中的文件
js获取要删除的id 批量删除的小例子
php删除目录下N天前所有文件的代码一例
自己动手制作jquery插件之自动添加删除行的实现
批处理概念与方法
mysql批量插入(insert)与批量更新(update)的例子
asp 保存数据与批量删除数据方法
全选、取消、批量删除的js代码

[关闭]
~ ~