批量刪除前端參數傳遞及后台接收


批量刪除,前台參數傳遞及后台參數接收

  后台采用數組接收

    例子情景:模擬批量刪除用戶

    思路:刪除用戶,每一個復選框的Value值都代表一個用戶的ID,獲取每一個選中的復選框的值,放入數組,然后直接

       傳遞給后台,在不知道一共有多少個復選框的時候,使用jQuery來實現

          var userIdList = new Array();//存放相應的用戶Id

       //給每一個選中的標簽都綁定一個方法

          $("input:checked").each(function(){

       //將標簽的值放入數組中

           userIds.push($(this).val());//此處添加不能使用add add不是一個function
         });

   

選擇 姓名 電話
何二 123
張三 123
李四 123
王五 123
趙六 123

    

    后台接收

        @RequestMapping(value="/reduceUser",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")

        @ResponseBody
        public Result deleteUser( @RequestParam("userIds[ ]")Integer [ ] userIds){
          List<Integer> userIdList = Arrays.asList(userIds);
          int num = this.userService.removeUser(userIdList);
          if(num==1){
            return new Result(200);
          }else{
            return new Result(400);
          }
        }

    前台頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
     <base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用戶的批量刪除</title>
        <style type="text/css">
            td{
                text-align: center;
            }
        </style>
        <script type="text/javascript" src="jquery/jquery-3.2.1.js" ></script>
        <script type="text/javascript" src="jquery/jquery-3.2.1.min.js" ></script>
        <script type="text/javascript">
                
                //用來全選和取消全部選擇
                function changeStatus(){
                    //獲取第一行 (選擇兩字旁邊 的那個復選框的狀態)
                    var flag = $("#cheooseBox").is(":checked");
                    $("input").attr("checked",flag);
                }
                //$("input[name='input1']").click(function(){
                $(function(){
                    //給提交的button綁定點擊事件
                    $("button").click(function(){
                        // checkedNum  = $("input[name='uname']:checked").length;
                        var checkedNum  = $("input:checked").length;
                        if(confirm("確定刪除所選項?")){
                            var userIds = new Array();
                            //給每一個選中的標簽都綁定一個方法
                            //$("input[name='uname']:checked").each(function(){
                            $("input:checked").each(function(){
                                //將標簽的值放入數組中
                                userIds.push($(this).val());//此處添加不能使用add  add不是一個function
                            });
                            /*    也可以直接使用用jQuery發送異步請求
                                $.post("user/reduceUser",{userIds:userIds},function(data){
                                if(data.status==200){
                                    //刪除成功
                                    if(confirm("恭喜你刪除成功!點擊確認刷新頁面")){
                                        //刪除成功直接從新發送請求,加載頁面
                                        $(location).attr("href","user/showUser");
                                    }    
                                }
                                },"json");
                            */
                            $.ajax({
                                type:"post",
                                url:"user/reduceUser",
                                data:{"userIds":userIdList},
                                dataType:"json",
                                //traditional:true,
                                success:function(data){
                                    if(data.status==200){
                                        if(confirm("恭喜你刪除成功!點擊確認刷新頁面")){
                          // 刪除成功后發送請求,刷新頁面 $(location).attr(
"href","user/showUser"); //window.location.href="user/showUser"; } } } }); } }); }); </script> </head> <body> <div class="user-info-form1" align="center"> <form action="" method="post"> <table border="1" cellspacing="0" cellpadding="0" width="300ox" height="450px"> <tr> <th>選擇 <input type="checkbox" id="cheooseBox" value="-1" onclick="changeStatus()" size="7" /> </th> <th>姓名</th> <th>電話</th> </tr> <tr> <td><input name="uname1" value="1" type="checkbox"/></td> <td>張三1</td> <td>123</td> </tr> <tr> <td><input name="uname2" value="2" type="checkbox"/></td> <td>張三2</td> <td>123</td> </tr> <tr> <td><input name="uname3" value="3" type="checkbox"/></td> <td>張三3</td> <td>123< </tr> <tr> <td><input name="uname4" value="4" type="checkbox"/></td> <td>張三4</td> <td>123</td> </tr> <tr> <td><input name="uname5" value="5" type="checkbox"/></td> <td>張三5</td> <td>123</td> </tr> </table> <button style="width:90px;height:50px">提交</button> </form> </div> </body> </html>

 

    

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM