[英]Update selected value in dropdown list depending on other lists selected values
[英]Get value of dropdown in JSP and load other contents depending on the value selected
我有一个JSP页面,其中包含下拉列表。 我使用列表类型的request属性填充此下拉列表,并遍历该下拉列表。
<select class="form-control" id="groupname" name="groupname">
<c:forEach items="${visibility}" var="groupListItem">
<option value="${groupListItem.groupName}">${groupListItem.groupName}</option>
</c:forEach>
</select>
每当我从下拉列表中选择一项时,我都需要在同一页面上显示复选框列表。 复选框的标签和选中/未选中值也是列表属性groupListItem
。
list属性是一个自定义bean,它具有一个映射,其中包含复选框名称和值,这些复选框和值决定是选中还是未选中。
选择下拉值时,如何获取复选框的值?
我的列表属性如下所示。
List<GroupVisibilityBean> groupList = driverDAO.getGroupVisibility();
model.addAttribute("visibility", createGroup());
private List<GroupVisibilityBean> createGroup() {
List<GroupVisibilityBean> groupList = new ArrayList<GroupVisibilityBean>();
GroupVisibilityBean bean1 = new GroupVisibilityBean();
bean1.setGroupName("GARDA");
Map<String, String> map1 = new HashMap<String, String>();
map1.put("Personal Details", "Y");
map1.put("Driver Details", "N");
GroupVisibilityBean bean2 = new GroupVisibilityBean();
bean2.setGroupName("COURT");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("Personal Details", "Y");
map2.put("Driver Details", "N");
groupList.add(bean1);
groupList.add(bean2);
return groupList;
}
有人可以在这里帮我吗?
您可能需要分别将创建的map1
和map2
添加到bean1
和bean2
。 现在,它们已分配了值,但您没有像
bean1.setCheckboxMap(map1);
帮助bean1
访问map1
等可能需要
当在bean中包含值时,需要将它们放入HTML页面。 最简单的方法是(例如,在foreach中<option>
下方)
<script> ${groupListItem} = {}; // create object of that name <c:forEach items="${groupListItem.checkboxMap}" var="mapItem"> ${groupListItem}["${mapItem.key}"] = "${mapItem.value}"; // set val </c:forEach> </script>
您可以将bean值存储在JS数组中,而不是使用key作为JS对象的键,如下所示:
<script> ${groupListItem} = {}; // create object of that name ${groupListItem}.keys = []; ${groupListItem}.values = []; <c:forEach items="${groupListItem.checkboxMap}" var="mapItem"> ${groupListItem}.keys.push("${mapItem.key}"); // append key ${groupListItem}.values.push("${mapItem.value}"); // append value </c:forEach> </script>
另请参见如何在使用哈希表的哈希表中迭代数组列表 。 如果您想付出更多努力,请使用JSON进行编码,如从javascript中读取jsp-variable-from 。
如果值是HTML格式,则需要更改复选框或在下拉列表更改后通过Javascript快速创建它们。 要检测dropdown事件,请参阅dropdown-using-javascript-onchange 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.