ajax+Jquery 封装+跳转

第一: 表格

<table>

<tr align="center">

<td><input type="text"  name="userID"class="userID"   value="${ user.userID}"   ></td>
<td><input type="text"name="userName"class="userName" value="${ user.userName}" ></td>
<td><input type="text" name="userCode" class="userCode" value="${ user.userCode}" ></td>
<td>
         <c:forEach items="${ menusList}" var="menu">

   <span style="width: 25%"> ${menu.menu_name}&nbsp;&nbsp;

             <input type="checkbox" name="menu_code" class="menu_code" value="${menu.menu_code}"  >

           </span>

        </c:forEach>
</td>
 <td>
  <c:forEach items="${ privilegeList}" var="privilege">

<span style="width: 25%"> ${privilege.preName}

                   <input type="checkbox"  name="pricode" class="pricode" value="${privilege.pricode}"  >

               </span>

  </c:forEach>
 </td>
 </tr>
     <tr align="center">
<td colspan="3"><input type="button" value="新增英雄霸业"  class="addNew"></td>
<td colspan="2"><input type="button" value="返回首页"     onclick="BACK()"></td>

</tr>

</ table>


第二: Jquery 点击触发函数

$(document).ready(function(){ 
//添加:  
$(".addNew").click(function() {
//菜单集合:
var menu_codeList = "";
    $('input:checkbox[name=menu_code]:checked').each(function(i){
      if(0==i){
      menu_codeList = $(this).val();
    }else{
      menu_codeList += (","+$(this).val());
    }
  });
    
    //权限集合:
    var pricodeList = "";
    $('input:checkbox[name=pricode]:checked').each(function(i){
      if(0==i){
    pricodeList = $(this).val();
    }else{
    pricodeList += (","+$(this).val());
    }
  });
  
var userID = $(".userID").val();                                    // 取对应组件的值

var userCode = $(".userCode").val();

if(userID!= null){
   var formData = new FormData();
    formData.append('userID', userID);
            formData.append('userCode', userCode);                  // “userCode” -- 对应 Controller 中的参数,保持一致
            formData.append('menu_codeList', menu_codeList);
            formData.append('pricodeList', pricodeList);

$.ajax({
url : "/ user/ump/addUserDoMenuPrivilege.action",        //工程名称+ Controller 路径

type : "POST",

data : formData,                                                    //对象 formData 保存数据

dataType: "json",

processData : false,

contentType : false,

success : function(data) {
alert("添加霸业成功");              //返回成功后继续跳转
window.location.href="${pageContext.request.contextPath}/ump/findUserMenuPrivilege.action?userID="+userID;
},
error : function(data) {
alert("添加霸业失败");
}
     });
}
});

});


猜你喜欢

转载自blog.csdn.net/word_joke/article/details/80405069
今日推荐