![](/img/trans.png)
[英]How to set the value to send to servlet from list of radio button in jsp
[英]Check Radio Button & Select Dropdown List According To Value Retrieved From Database in JSP
我的目标是从数据库中检索值并在JSP中显示。
单选按钮
如果数据库数据是所有者,将检查所有者单选按钮。 如果数据库数据是收银员,则将检查收银员。
下拉列表
如果数据库数据为橙色,则将选择橙色选项。
以下是我的代码。
帮助将是欣赏。 谢谢! :)
单选按钮
<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>
对于DropDown,假设您的数据在staffFruit下的bean中是相同的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梯形图。 我建议你使用更方便的东西
你可以使用jsp的简单划线来做得更好。
单选按钮:
<%
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 %> />
对于下拉:
<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>
试试这个并通知我是否需要进一步的协助。
更改
${staff.staffRole} == "Owner"
至
${staff.staffRole == "Owner"}
您可以使用JSTL eq
运算符
<c:if out="${staff.staffRole eq 'Owner'}"> ......
要么
<c:if out="${staff.staffRole == 'Owner'}"> .....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.