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