检查单选按钮,根据从JSP中的数据库检索到的值选择下拉列表

时间:2020-11-28 14:13:06

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

我的目标是从数据库中检索值并在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.

如果数据库数据是Owner,则将检查Owner单选按钮。如果数据库数据是出纳,则对出纳进行检查。

Dropdown list

下拉列表中

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

如果数据库数据是橙色的,那么将选择Orange选项。

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>

4 个解决方案

#1


3  

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

对于下拉,假设您的数据在staffFruit的相同bean中

            <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

这是一个简单的*。我建议你用一些更方便的,比如

#2


0  

You can do it better using simple scriplet of jsp.

使用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.

试试这个,如果需要进一步的帮助,请告诉我。

#3


0  

Change

改变

${staff.staffRole} == "Owner"

To

${staff.staffRole == "Owner"}

#4


0  

You can use JSTL eq operator

您可以使用JSTL eq操作符

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

Or

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

#1


3  

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

对于下拉,假设您的数据在staffFruit的相同bean中

            <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

这是一个简单的*。我建议你用一些更方便的,比如

#2


0  

You can do it better using simple scriplet of jsp.

使用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.

试试这个,如果需要进一步的帮助,请告诉我。

#3


0  

Change

改变

${staff.staffRole} == "Owner"

To

${staff.staffRole == "Owner"}

#4


0  

You can use JSTL eq operator

您可以使用JSTL eq操作符

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

Or

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