如何设置默认值Combobox JSP


问题内容

我想设为默认 value= ${theObject2.countryId}

<select id="locationCountry" style="padding-left: 2em" >
<option value="">Country</option>
<c:forEach  items="${countrieDetail}" var="theObject" varStatus="theCount">
<option value="${theObject.countryId}" >${theObject.countryName}</option>
</c:forEach>
</select>

问题答案:

您可以使用HTML选择的Attribute。只需比较ID,然后将其设置为匹配即可。

样例代码:

<select id="locationCountry" style="padding-left: 2em">
    <option value="">Country</option>
    <c:forEach items="${countrieDetail}" var="theObject">
        <option value="${theObject.countryId}" <c:if test="${theObject.countryId == theObject2.countryId }">selected</c:if>>
              ${theObject.countryName}
        </option>
    </c:forEach>
</select>