没有梦想,何必远方

批量删除前端参数传递及后台接收

批量删除,前台参数传递及后台参数接收

  后台采用数组接收

    例子情景:模拟批量删除用户

    思路:删除用户,每一个复选框的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>

 

    

  

posted @ 2017-12-02 21:30  北极丶光  阅读(12785)  评论(0编辑  收藏  举报