我正在尝试根据从数据库返回的值来检查一个复选框。到目前为止,我在我的 Controller 层上得到了正确的“Y”或“N”值,但是复选框没有被选中/取消选中。我尝试了以下但它不起作用,我认为测试场景有问题。
<td class="normalTD " width="4%" style="white-space: nowrap" align="left" >
<c:choose>
<c:when test="detail[${loop.index}].flag eq 'Y'">
<input type="checkbox" path="detail[${loop.index}].flag" id="detail[${loop.index}].flag" checked="checked" />
</c:when>
<c:otherwise>
<input type="checkbox" path="detail[${loop.index}].flag" id="detail[${loop.index}].flag" checked="" />
</c:otherwise>
</c:choose>
</td>
在复选框之前循环:
<c:set var="detail" value="${commandObj.detail}" scope="page" />
<c:if test="${!empty commandObj.detail}">
<c:forEach items="${commandObj.detail}" var="detail" varStatus="loop">
<c:if test="${!empty detail.cd}">
最佳答案
它有点旧,所以我想你已经找到了解决方案,但如果没有:
您可能需要检查 forEach 循环中的 detail
变量的标志,detail
不是 commandObj.detail
而是此列表/数组中的实例。您应该将 commandObj.detail
重命名为 commandObj.details
以区分实例和集合 ;)
此外,您应该使用三元条件来获得更具可读性的代码。条件很简单。
<c:forEach items="${commandObj.detail}" var="detail" varStatus="loop">
<c:if test="${!empty detail.cd}">
<input type="checkbox"
path="detail[${loop.index}].flag"
id="detail[${loop.index}].flag"
${"detail.flag eq 'Y'" ? "checked" : ""}
/>
选中的属性不需要值,只需要存在或不存在即可。
我现在无法测试它写,如果有错误我今晚忘记检查了,谢谢通知我。 :) (如果有这一行,我忘记了;))
关于html - 根据返回的数据库值选中/取消选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36221748/