简体   繁体   中英

Check Radio Button & Select Dropdown List According To Value Retrieved From Database in JSP

My goal is to retrieve value from database and show in JSP.

Radio button

If the database data is Owner, the Owner radio button will be check. If the database data is Cashier, the Cashier will then be check.

Dropdown list

If the database data is Orange, then the Orange option will be selected.

Below are my codes.

Help will be appreciate. Thanks! :)

Radio button

<input type="radio" name="role" id="Owner" value="Owner" <c:if out='${staff.staffRole} == "Owner"'>checked</c:if>/>

<input type="radio" name="role" id="Cashier" value="Cashier" <c:if out='${staff.staffRole} == "Cashier"'>checked</c:if>/>

Dropdown list

<select class="form-control">
    <option>Apple</option>
    <option>Orange</option>
    <option>Durian</option>
</select>

For Radio Buttons:

    <c:choose>
  <c:when test='${staff.staffRole == "Owner"}'>
    <input type="radio" name="role" id="Owner" value="Owner" checked >
  </c:when>
<c:otherwise>
  <input type="radio" name="role" id="Owner" value="Owner">
</c:otherwise>
</c:choose>
<c:choose>
  <c:when test='${staff.Cashier} == "Owner"}'>
    <input type="radio" name="role" id="Cashier" value="Cashier" checked >
  </c:when>
  <c:otherwise>
    <input type="radio" name="role" id="Cashier" value="Cashier" value="Owner">
  </c:otherwise>
</c:choose>

For DropDown assuming your data is in bean same bean under staffFruit

            <select class="form-control">
            <c:choose>
                  <c:when test='${staff.staffFruit == "Apple"}'>
                    <option selected>Apple</option>
                  </c:when>
                <c:otherwise>
                  <option>Apple</option>
                </c:otherwise>
            </c:choose>
            <c:choose>
                  <c:when test='${staff.staffFruit == "Orange"}'>
                    <option selected>Orange</option>
                  </c:when>
                <c:otherwise>
                  <option>Orange</option>
                </c:otherwise>
            </c:choose>
            <c:choose>
                  <c:when test='${staff.staffFruit == "Durian"}'>
                    <option selected>Durian</option>
                  </c:when>
                <c:otherwise>
                  <option>Durian</option>
                </c:otherwise>
            </c:choose>
            </select>

This is a simple if else ladder. I would recommend you to use something more convenient one like

You can do it better using simple scriplet of jsp.

Radio-buttons:

<%
String ownerChecked = "";
String cashierChecked = "";
if(staff.staffRole.equals("Owner")){
    ownerChecked = "checked";
}else{
    cashierChecked = "checked";
}
%>

<input type="radio" name="role" id="Owner" value="Owner" <%=ownerChecked %> />
<input type="radio" name="role" id="Cashier" value="Cashier" <%=cashierChecked %> />

For Dropdown:

<select class="form-control">
    <option selected="<%=staff.staffFruit.equals("Apple") %>">Apple</option>
    <option selected="<%=staff.staffFruit.equals("Orange") %>">Orange</option>
    <option selected="<%=staff.staffFruit.equals("Durian") %>">Durian</option>
</select>

Try this and inform me if further assitance is required.

Change

${staff.staffRole} == "Owner"

To

${staff.staffRole == "Owner"}

You can use JSTL eq operator

<c:if out="${staff.staffRole eq 'Owner'}"> ......

Or

<c:if out="${staff.staffRole == 'Owner'}"> .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM