如何使用JSTL设置对特定值选中的复选框? [英] How do i set check-boxes checked on particular Value using JSTL?
问题描述
我正在使用JSTL和多个复选框,我只想默认选中一些复选框
I am using JSTL and multiple check-boxes, i just want to checked some check-boxes by default
<form:checkboxes path="userName" items="${UserList}" id="userName" class="check-margin-top"/>
我就是这样
<form:checkboxes path="userName" items="${UserList}" id="userName"class="check-margin-top"
<c:forEach var='list' items="${UserList}">
<c:if test="${list == '1'}"checked="checked"></c:if>
</c:forEach>
></form:checkboxes>
我正在获得像这样的UserList {4 = A,11 = DUMMY,9 =测试,5 = John Smith,6 = kp } 我现在想检查9和5,该怎么办?
And i am getting UserList like this { 4 = A,11 = DUMMY,9 = Test,5 = John Smith,6 = kp } i want to keep checked to 9 and 5 now what should i do ?
推荐答案
这是您问题的答案.
生成复选框值的运行时列表,并将其链接到Spring的表单标签
<form:checkboxes>
//SimpleFormController...
protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
List<String> userList= new ArrayList<String>();
webFrameworkList.add("John");
webFrameworkList.add("Smith");
webFrameworkList.add("Doe");
webFrameworkList.add("Peter");
referenceData.put("userList", userList);
return referenceData;
}
默认选中... 如果要选中两个复选框,默认情况下值为"John"和"Smith",则可以使用值"John"和"Smith"初始化"favUser"属性.例如:
Checked by default… If you want to make 2 checkboxes with value "John" and "Smith" are checked by default, you can initialize the "favUser" property with value "John" and "Smith". For example :
//SimpleFormController...
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
User user = new User();
user .setFavUser(new String []{"John","Smith"});
return user ;
}
User.java
User.java
public class User{
String [] favUser;
//getter & setter
}
您的复选框应该是
your checkboxes should be
<form:checkboxes items="${userList}" path="favUser" />
注意
<form:checkboxes items="${dynamic-list}" path="property-to-store" />
对于多个复选框,只要"path"或"property"值为 等于任何复选框值– $ {dynamic-list}", 复选框将被自动选中.
For multiple checkboxes, as long as the "path" or "property" value is equal to any of the "checkbox values – ${dynamic-list}", the matched checkbox will be checked automatically.
来自 https://www.mkyong. com/spring-mvc/spring-mvc-checkbox-and-checkboxes-example/
这篇关于如何使用JSTL设置对特定值选中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!