首页 > 解决方案 > 在 Thymeleaf 中选中的复选框属性

问题描述

如何使用 Thymeleaf 为表单中的输入设置选中的属性?这是我的代码,目前不起作用:

<label th:each="cat : ${categories}">
    <input type="checkbox" value="" 
        th:value="${cat.id}" 
        th:text="${cat.description}"
        th:checked="${recipe.getCategories().contains(cat) ? true : false}"
    />
</label>

标签: spring-mvcthymeleaf

解决方案


如评论中所述,问题可能来自其他地方,但试试这个,看看它是否有帮助:

th:checked="${recipe.getCategories().contains(cat)}"

推荐阅读