springMVC回收站多選(checkbox)刪除
阿新 • • 發佈:2018-11-12
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) { } } }