jquery ajax 實現大量刪除

來源:互聯網
上載者:User

js頁面jquery代碼:


[html]
// 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> 
<td><input type="checkbox" name="subChk" value="${user.id}"/></td> 

    // 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>
  <td><input type="checkbox" name="subChk" value="${user.id}"/></td>

回呼函數,在請求完成後需要進行的操作:此處是把選中的checkbox去掉(因為是用到了freemarker的list迴圈,去掉是資料後checkbox序號變化,還有有相應未知的checkbox被選中,需要去掉)。


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

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

 

[html]
@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"; 
    } 

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

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.