spring - 使用两个字符串集合检查 Thymeleaf 复选框
问题描述
我有两个字符串集合。
一种是生成所有复选框。
另一种是选择要检查的元素。
例如,我有两个字符串集合 ["A", "B", "C", "D"] ["A", "C"]
我想做 4 个复选框 A、B、C、D 并想检查 A 和 C
我尝试如下。
<span th:each="interestName : ${allInterest}">
<span th:each="interest : ${userInfo.interestName}">
<input type="checkbox"
name="eachUserInterest"
th:value="${interestName.name}"
th:checked="${interestName.name.equals(interest)}"/>
<label th:text="${interestName.name}"></label>
</span>
</span>
但是,上面的代码会重复生成一个复选框,其次数与选中的次数一样多。 执行结果
请帮帮我。
解决方案
您应该只遍历allInterest
数组,并使用contains
来确定是否选中了复选框。例如这样的事情:
<th:block th:each="interest: ${allInterest}">
<input type="checkbox"
name="eachUserInterest"
th:value="${interest}"
th:checked="${userInfo.interestName.contains(interest)}" />
<label th:text="${interest}"></label>
</th:block>
推荐阅读
- azure-service-fabric - 如何将外部(打包)Service Fabric 服务添加到应用程序清单
- ios - 插入耳机后我只能听到录音机音频
- python-3.x - 奇异矩阵的逆
- vba - 如何在 VBA 中创建一个全局数组?
- java - 将函数作为参数传递给方法并在Java中返回其返回值?
- php - 使用加密创建基于电子邮件的密码重置功能
- jquery - 如果选中其他任何其他复选框,如何选中第一个复选框,如果未选中所有其他复选框,则取消选中第一个复选框
- java - 为什么我的所有页面数据都显示 StaleElementReferenceException
- interrupt - PIC timer1 中断永远不会发生
- java - JDBI测试自定义注解生成sql语句