ajax做批量删除,jquery ajax实现批量删除具体思路及代码

2023-05-16

推荐:ajax中文乱码问题解决方案ajax中文乱码问题在中文中经常会出现这种问题,其实只要稍加注意就不会出现ajax中文乱码这回事情了,接下来为大家详细介绍下如何解决这类问题

复制代码 代码如下:www.mb5u.com

// 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();

}

});

}

});

});

页面元素:

删除用户

全选

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

复制代码 代码如下:www.mb5u.com

success: function(result) {

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

window.location.reload();

}

java后台代码:

复制代码 代码如下:www.mb5u.com

@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";

}

效果图:

0ba4bb78d428fdbfb95054ad3bbac5d4.png

分享:AJAX避免用户重复提交请求实现方案为了避免因某些原因用户同时多次点击按钮,提交重复的请求,我们需要禁用请求提交按钮,接下来与大家一起分享下实现方法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ajax做批量删除,jquery ajax实现批量删除具体思路及代码 的相关文章

随机推荐