动态checkbox 问题

liuc0317 2011-01-06 05:21:20
我现在做的一个东西需要在做更新的时候把从数据库取出的checkbox选中。
从数据库取出的是一个cIndustrys.(当前行业)
页面中把所有的checkbox都列出(行业)。如果条件相同就checked="checked"
问题:当所把所有的行业都列出,(所有行业 industrys)行业 父类 和子类,当前行业是包括父子类的。
怎么选中:
代码如下:但会输出两次

<c:forEach items="${cIndustrys}" var="cIndustry">
<c:set value="false" var="isEmpty"></c:set>
<c:set value="false" var="isEmpty2"></c:set>
<c:forEach items="${industrys}" var="industry">
<c:choose>
<c:when test="${cIndustry.industryId==industry.industryId}">
<input type="checkbox" class="check" checked="checked" name="industry" value="${industry.industryId}">${industry.industryCnName}</br>
</c:when>
<c:otherwise>
<input type="checkbox" class="check" name="industry" value="${industry.industryId}">${industry.industryCnName}</br>
</c:otherwise>
</c:choose>
<c:forEach items="${industry.subIndustries}" var="sub">
<c:choose>
<c:when test="${cIndustry.industryId==sub.industryId}">
    <input type="checkbox" class="check" checked="checked" name="industry" value="${sub.industryId}">${sub.industryCnName}</br>
</c:when>
<c:otherwise>
    <input type="checkbox" class="check" name="industry" value="${sub.industryId}">${sub.industryCnName}</br>
</c:otherwise>
</c:choose>

</c:forEach>

</c:forEach>

</c:forEach>
...全文
91 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayanyun1986 2011-01-07
  • 打赏
  • 举报
回复
感觉设计有点乱,用简单的方法处理复杂的问题。
wangjiangbo2 2011-01-07
  • 打赏
  • 举报
回复
你的业务逻辑没看明白  
<c:choose>
<c:when test="${cIndustry.industryId==sub.industryId}">
    <input type="checkbox" class="check" checked="checked" name="industry" value="${sub.industryId}">${sub.industryCnName}</br>
</c:when>
<c:otherwise>
    <input type="checkbox" class="check" name="industry" value="${sub.industryId}">${sub.industryCnName}</br>
</c:otherwise>
</c:choose>

出现了2次 是什么意思?
peng_tp 2011-01-07
  • 打赏
  • 举报
回复
<input type="checkbox" name="menu" <c:forEach items="${editlist}" var="edit"><c:if test="${edit.fname =='数据维护'}">checked="checked" </c:if></c:forEach>
value="数据维护|left_admin/sjwh.htm" style="border:none" />
这是我之前做的没有问题
wangjiangbo2 2011-01-07
  • 打赏
  • 举报
回复
<input type="checkbox" name="zy" value="<%=id2 %>" <%if(zy.indexOf(id2)!=-1){%>checked<%} %>/>

我一般都是这样判断,不喜欢用JSTL,zy是后台传过来的字符串
liuc0317 2011-01-07
  • 打赏
  • 举报
回复
当是 条件==true checked="checked"
否则没有
就是动态的把所需要的checkbox选中

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧