1、js代码:
<%
// 根路径取得
String root = request.getContextPath();
//删除按钮调用路径
String deleteAction = root + "/b019/b019001/deleteAll.go";
%>
<form action="">
<input type="checkbox" name="selectall2" id='x'/><label for="x">一键全选</label><button onclick="batchDeletes()">删除</button><br>
<input type="checkbox" name="stuCheckBox" value="sn1">我有一辆自行车<br>
<input type="checkbox" name="stuCheckBox" value="sn2">我有一辆汽车<br>
<input type="checkbox" name="stuCheckBox" value="sn3">我有一架飞机<br>
</form>
<script>
//$('input[name="selectall2"]').click(function(){
//$('input[name="selectall2"]').bind("change",function(){
$('input[name="selectall2"]').on("click",function(){
alert(this.checked);
if($(this).is(':checked')){
$('input[name="stuCheckBox"]').each(function(){
//此处如果用attr,会出现第三次失效的情况
$(this).prop("checked",true);
});
}else{
$('input[name="stuCheckBox"]').each(function(){
$(this).removeAttr("checked",false);
//$(this).removeAttr("checked");
});
}
});
function batchDeletes(){
//判断至少写了一项
var checkedNum = $("input[name='stuCheckBox']:checked").length;
if(checkedNum==0){
alert("请至少选择一项!");
return false;
}
if(confirm("确定删除所选项目?")){
var checkedList = new Array();
$("input[name='stuCheckBox']:checked").each(function(){
alert($(this).val());
checkedList.push($(this).val());
});
alert(checkedList.toString());
$.ajax({
type:"POST",
url:'<%=deleteAction%>',
data:{"delitems":checkedList.toString()},
datatype:"html",
success:function(data){
$("[name='selectall2']:checkbox").attr("checked",false);
location.reload();//页面刷新,或者表单提交
},
error:function(data){
art.dialog.tips('删除失败!');
}
});
}
}
</script>
2、controller写法:
@RequestMapping("/deleteAll.do")
@ResponseBody()
public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {
String items = request.getParameter("delitems");// System.out.println(items);
String[] strs = items.split(",");
for (int i = 0; i < strs.length; i++) {
try {
// int a = Integer.parseInt(strs[i]);
String a = strs[i];
persistService.delStudentBySn(a);
} catch (Exception e) {
}
}
}