I have form with select box in my user form. I also need to update the form in edit mode. I am able to get selected value in edit mode. But I am unable to set the selected value in the edit mode.
Here I am able to get the selected value from db. <%=user.getTitle() %>
Now how can I set the selected value in my select box.
<aui:select name="title">
<aui:option label="Dr" value="dr" />
<aui:option label="Mr" value="mr" />
<aui:option label="Mrs" value="mrs" />
<aui:option label="Ms" value="ms" />
</aui:select>
Example I aset the selected value for input field like this,
<aui:input name="emailAddress" value=""></aui:input>
Any suggestions please!!
You can set selected value as:
Hard-coded Options:
<aui:select name="title">
<aui:option label="Dr" value="dr" selected="<%=user.getTitle().equals("dr") %>" />
<aui:option label="Mr" value="mr" selected="<%=user.getTitle().equals("mr") %>" />
<aui:option label="Mrs" value="mrs" selected="<%=user.getTitle().equals("mrs") %>" />
<aui:option label="Ms" value="ms" selected="<%=user.getTitle().equals("ms") %>" />
</aui:select>
Dynamic Options:
<aui:select name="title">
<%for(int i = 0; i < listOfOptions.size(); i++){
Object option = listOfOptions.get(i);
boolean selected = false;
if(user.getTitle().equals(option.getTitle())){
selected = true;
} %>
<aui:option label=<%=option.getTitle() %> value="<%=option.getValue() %>" selected=<%=selected %> />
<%} %>
</aui:select>
A cleaner way to do it:
<aui:select name="title">
<c:forEach items="${listOfOptions}" var="currOption">
<option value="${currOption}"<c:if test="${currOption == title}"> selected="selected"</c:if>>
${currOption}
</option>
</c:forEach>
</aui:select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With