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

标签 java database jsp radio-button html-select

我的目标是从数据库中检索值并在 JSP 中显示。

单选按钮

如果数据库数据是所有者,则所有者单选按钮将被选中。如果数据库数据是Cashier,那么Cashier就会被check。

下拉列表

如果数据库数据为橙色,则选择橙色选项。

下面是我的代码。

帮助将不胜感激。谢谢! :)

单选按钮

<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>/>

下拉列表

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

最佳答案

对于单选按钮:

    <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>

对于下拉菜单 假设您的数据在 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>

这是一个简单的 if else 阶梯。我建议你使用更方便的东西,比如

关于java - 根据从 JSP 中的数据库检索的值检查单选按钮并选择下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857383/

相关文章:

java - 带有嵌入式 jetty 的 Servlet 3.0 文件上传(@MultipartConfig)

java - 如何在处理中创建单个草图的多个窗口?

ios - sqlite ios : attempt to write a readonly database

java - 使用 Maven 创建 EAR 文件

java - 在 webview Android 中打开新 url 时保持 Session 属性

java - CardView在不同屏幕密度下无法正确显示?

不支持 java.util.Date

php - 链接数据库 ID 和文件

php - Mysql:LAST_INSERT_ID按预期工作(由我)?

spring - 如何在 Spring 中以编程方式解析属性占位符