java - jsp下拉菜单没有自动选择正确的值
问题描述
我在 jsp 页面上有一个主要是自动填充的下拉列表。提交表单后,当页面重新加载时,搜索结果会显示在下表中,并且所有下拉列表和其他字段条目都会像提交之前一样重新填充。
除了这一值<option value="A"><spring:message code="code.label.ANE" /></option>
。它不像其他人。它不是从数据库动态创建的,因为它基本上只是将多个结果合并为一个,因此不是有效的数据库代码。其他人将正确地重新选择,但不是这个。
我认为<option value="">${Select}</option>
这只有效,因为它是第一个值,因此默认情况下被选中。无法实际测试它,因为该值不支持任何搜索。
不,我的值不能成为新的默认值,因为它不太可能是唯一的自定义报告选项。
<div class="col-md-4">
<label>
<spring:message code="common.label.lifeCycle" />
</label>
<form:select path="hrmsLifeCycleCode" id="hrmsLifeCycleCode" cssClass="form-control">
<spring:message code="field.select" var="Select" />
<option value="">${Select}</option>
<option value="A"><spring:message code="code.label.ANE" /></option>
<form:options items="${hrmsLifeCycle}" itemValue="codeValue" itemLabel="longDesc" />
</form:select>
</div>
解决方案
在jsp中添加以下内容
$(document).ready(function() {
<c:if test="${querySelect == 'A'}">
$("#hrmsLifeCycleCode").val('A');
</c:if>
});
在控制器中
model.addAttribute("querySelect", hrmsLifeCycle.getHrmsLifeCycleCode());
As the controller is now passing the selected value, when the selected value is this one case thats not being auto-selected it will select it after the page has been loaded.
这不是最好的解决方案,但它有效。
推荐阅读
- kubernetes - K3S:如何通过 hub.docker.com 进行身份验证也拉取私有图像?
- javascript - 使用 MVC 的 JQueryFullCalendar
- xcode - 在 Xcode 12 中运行签名后脚本
- javascript - 获取相对于旋转 div 上 div 浏览器窗口的 x 和 y
- java - java.lang.Error:从 assets 文件夹中复制 SQLite 数据库中的数据库时出错
- java - 在 Eclipse 插件中加密敏感数据
- salesforce - Salesforce 沙盒中不提供社区选项
- javascript - 在 json api 请求中附加我自己的 json
- sql - IN SQL INNER JOIN 可以添加两个不同列的表吗?
- android - 为什么我在 ArrayAdapter 中遇到此错误?