手机
当前位置:查字典教程网 >编程开发 >AJAX相关 >jquery ajax实现批量删除具体思路及代码
jquery ajax实现批量删除具体思路及代码
摘要:js页面jquery代码:复制代码代码如下://JavaScriptDocument$(document).ready(function()...

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><input type="checkbox" id="allChk"/>全选</th>

<td><input type="checkbox" name="subChk" value="${user.id}"/></td>

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

复制代码 代码如下:

success: function(result) {

$("[name = 'items']:checkbox").attr("checked", false);

window.location.reload();

}

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 ajax实现批量删除具体思路及代码1

【jquery ajax实现批量删除具体思路及代码】相关文章:

利用ajax实现简单的注册验证局部刷新实例

ajax验证用户名和密码的实例代码

妙用Ajax技术实现局部刷新商品数量和总价实例代码

jquery中的ajax异步上传

Ajax实现跨域访问的三种方法

Ajax实现无闪烁定时刷新页面实例代码

asp+ajax实现静态页面分页的代码

jquery中的ajax同步和异步详解

利用Ajax实现在脚本里传值实例介绍

jQuery中ajax - get() 方法实例详解

精品推荐
分类导航