例1:
<select id="select" style="height:28px" name="categoryId">
<c:forEach items="${categorys}" var="category">
<c:if test="${blog.categoryId==category.id}">
<option value="${category.id }" selected>${category.name }</option>
</c:if>
<c:if test="${!(blog.categoryId==category.id)}">
<option value="${category.id }" >${category.name }</option>
</c:if>
</c:forEach>
</select>
备注:例如 传到前台blog和categorys
其中blog有所属分类的id,所有分类都有id。两者相等时,显出当前分类
例2:性别下拉
默认数据库中 1是男,0时女;
<select style="width:100px;">
<c:if test="${user.gender==1}">
<option value="1">男</option>>
</c:if>
<c:if test="${!(user.gender==1)}">
<option value="0">女</option>
</c:if>
</select>
此处注意下拉框的样式修改
本文介绍如何使用JSP和EL表达式在网页上动态生成带有预选选项的下拉框。通过遍历后端传递的数据集,根据不同条件设置下拉选项是否被选中。
2884

被折叠的 条评论
为什么被折叠?



