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

2016-02-19 09:36 320 1 收藏

今天图老师小编给大家介绍下jquery ajax实现批量删除具体思路及代码,平时喜欢jquery ajax实现批量删除具体思路及代码的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - Web开发 】

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
tdinput 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";
}

效果图:

来源:https://www.tulaoshi.com/n/20160219/1591430.html

延伸阅读
当屏幕变为横屏的时候,系统会重新呼叫当前Activity的OnCreate方法,你可以把以下方法放在你的OnCreate中来检查当前的方向,然后可以让你的SetContentView来载入不同的Layout xml. 代码如下: if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { Log.i("info", "landscape"); } else if (th...
标签: Web开发
到了B/S开发时代,网页前端布局也把Tab页的布局形式吸收了过来。特别是和Ajax技术结合起来,可以更充分发挥Tab页的良好表现力和数据缓存的优势,是一种良好的网页布局形式。 网上的Tab页代码很多,实现也大同小异 ,但代码都显得凌乱,若要真的用的话,必须费劲的吃透它,才能进行二次定制开发,为我所用。实际上实现这个Tab页界面非常简单,仅...
标签: Web开发
AJAX介绍      AJAX 关键词:   JavaScript脚本和可扩展标记语言(XML)   WEB浏览器技术   开放式WEB标准   浏览器以及独立平台   更好更快的网络应用程序   XML以及HTTP请求      AJAX = 异步JavaScript和可扩展标记语言   A...
使用样式文件,在values 目录下新建styles.xml文件,编写如下代码: 代码如下: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--resources style name="dialog" parent="@android:style/Theme.Dialog" item name="android:windowFrame"@null/item item name="android:windowIsFloati...
标签: Web开发
代码如下: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head title/title script src="js/Jquery1.7.js" type="text/javascript"/script script type="text/javascript" $(function () { $('#Button1').cl...

经验教程

129

收藏

85
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部