jquery ajax實(shí)現(xiàn)批量刪除具體思路及代碼_AJAX教程

      編輯Tag賺U幣

      推薦:ajax中文亂碼問題解決方案
      ajax中文亂碼問題在中文中經(jīng)常會(huì)出現(xiàn)這種問題,其實(shí)只要稍加注意就不會(huì)出現(xiàn)ajax中文亂碼這回事情了,接下來為大家詳細(xì)介紹下如何解決這類問題

      js頁面jquery代碼
      復(fù)制代碼 代碼如下:www.wf0088.com

      // JavaScript Document
      $(document).ready(function() {
      // 全選
      $("#allChk").click(function() {
      $("input[name='subChk']").});
      // 單選
      var subChk = $("input[name='subChk']")
      subChk.click(function() {
      $("#allChk").prop("checked", subChk.length == subChk.filter(":checked").length ? true:false);
      });
      /* 批量刪除 */
      $("#del_model").click(function() {
      // 判斷是否至少選擇一項(xiàng)
      var checkedNum = $("input[name='subChk']:checked").length;
      if(checkedNum == 0) {
      alert("請(qǐng)選擇至少一項(xiàng)!");
      return;
      }
      // 批量選擇
      if(confirm("確定要?jiǎng)h除所選項(xiàng)目?")) {
      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>
      <td><input type="checkbox" name="subChk" value="${user.id}"/></td>

      回調(diào)函數(shù),在請(qǐng)求完成后需要進(jìn)行的操作:此處是把選中的checkbox去掉(因?yàn)槭怯玫搅薴reemarker的list循環(huán),去掉是數(shù)據(jù)后checkbox序號(hào)變化,還有有相應(yīng)未知的checkbox被選中,需要去掉)。
      復(fù)制代碼 代碼如下:www.wf0088.com

      success: function(result) {
      $("[name = 'items']:checkbox").attr("checked", false);
      window.location.reload();
      }

      java后臺(tái)代碼
      復(fù)制代碼 代碼如下:www.wf0088.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";
      }

      效果圖:

      分享:AJAX避免用戶重復(fù)提交請(qǐng)求實(shí)現(xiàn)方案
      為了避免因某些原因用戶同時(shí)多次點(diǎn)擊按鈕,提交重復(fù)的請(qǐng)求,我們需要禁用請(qǐng)求提交按鈕,接下來與大家一起分享下實(shí)現(xiàn)方法

      來源:模板無憂//所屬分類:AJAX教程/更新時(shí)間:2013-04-22
      相關(guān)AJAX教程