首页 > 解决方案 > 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>

标签: javaspringjsp

解决方案


在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.

这不是最好的解决方案,但它有效。


推荐阅读