[英]How to make an option selected from a dropdown list in jsp?
In my project. 在我的项目中。 I want to populate the drop down list on a jsp from a database. 我想从数据库填充jsp上的下拉列表。
<select id="names" name="names"> <c:forEach items="${names}" var="names">
<option><c:out value="${names}"/></option>
</c:forEach>
</select>
The ${names}
is a list of names from the database. ${names}
是数据库中的名称列表。 I want to select an option dynamically in the drop down list. 我想在下拉列表中动态选择一个选项。 Suppose there are three names in the database Rohan, Dean, Justin. 假设数据库中有三个名字Rohan,Dean,Justin。 If Dean is logged, i want select the option Dean as selected. 如果记录了Dean,我想选择Dean选项。
I try a code like this but this does not work. 我尝试这样的代码,但这不起作用。
<option value="${names}" ${names == names ? 'selected' : ''}>${names}</option>
Try like this assuming that loggedInUser variable holds the String value of the currently logged in user. 假设loggedInUser变量保存当前登录用户的String值,请尝试这样做。
<select id="names" name="names">
<c:forEach items="${names}" var="names">
<c:when test="${loggedInUser eq names}">
<option value ="<c:out value="${names}"/>" selected="selected">${names}</option>
</c:when>
<c:otherwise>
<option><c:out value="${names}"/></option>
</c:otherwise>
</c:forEach>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.