Home > Web Front-end > JS Tutorial > How to get the value of the multi-select box value in a post in SpringMVC (code example)

How to get the value of the multi-select box value in a post in SpringMVC (code example)

亚连
Release: 2018-06-12 20:20:38
Original
3192 people have browsed it

Now I will share with you an article about the value of the post checkbox multi-select box value in SpringMVC (hidden field method). It has a good reference value and I hope it will be helpful to everyone.

I pass the value of the checkbox multi-select box to the backend through a string. First, I call js to traverse the selected checkbox to obtain the selected box value, then write it into the hidden field, and finally write the composition object. Property submission. See code: `

Frontend:

<form:form commandName="user" method="post">
     <c:forEach items="${deploys}" var="deploy" varStatus="deployStatus">
      <input type="checkbox" name="checkbox" value="${deploy.id}"/>${deploy.systemName}
      <br>
     </c:forEach>
     <spring:bind path="id">//实际存储的值,此处隐藏
      <p class="form-group" hidden>
       <form:input path="id" name="id" cssClass="form-control"></form:input>
      </p>
     </spring:bind>
     <spring:bind path="accessControl">//实际存储的值,此处隐藏
      <p class="form-group" hidden>
       <form:input path="accessControl" name="accessControl" cssClass="form-control"></form:input>
      </p>
     </spring:bind>
     <input type=&#39;button&#39; value=&#39;确定&#39; onclick="fun()"/>//调用脚本,给需要post的数据赋值
     <p class="form-group">
      <button type="submit" class="btn btn-primary">保存</button>
      <a class="btn btn-success pull-right" href="/user/list" rel="external nofollow" >返回</a>
     </p>
 </form:form>
Copy after login

Script:

<script type="text/javascript">
 function fun() {
  var boxes = document.getElementsByTagName("input");
//  var val = []
  var str = "";
  for (var i = 0; i < boxes.length; i++) {
   if (boxes[i].name == "checkbox" && boxes[i].checked == true) {
//    val.push(boxes[i].value);
    str += boxes[i].value + &#39;,&#39;;
   }
  }
  $("#accessControl").val(str)
//  alert(atr);
//  alert(val);
 }
</script>
Copy after login

Backend:

 @RequestMapping(value = "editaccesscontrol", method = RequestMethod.POST) //后端方法,
 public String editAccessControlPost(User user,ModelMap model ) {//接收参数对象user
  userMapper.updateUserAccessControl(user);
  model.addAttribute("user",user);
  model.addAttribute("success", "权限修改成功");
  return "redirect:/user/editaccesscontrol?id="+user.getId();
 }
Copy after login

The above article briefly talks about the post checkbox multi-select box value in SpringMVC (hidden field method) is what I share with you. All the content

Related articles:

How to upload and compress images in js (detailed tutorial)

How to use vue Implementing CSS transition effects

How to use WeChat applet to implement image upload function

The above is the detailed content of How to get the value of the multi-select box value in a post in SpringMVC (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template