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='button' value='确定' 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>
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 + ','; } } $("#accessControl").val(str) // alert(atr); // alert(val); } </script>
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(); }
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!