问题描述
限时送ChatGPT账号..我一直在玩 spring-boot 和 thymeleaf;我正在尝试做一个表格,我将列出数字并且用户将选择它们;但是我想检查"第三个元素(当等于 3 时)并且我看不到输入被检查.
I've been play around with spring-boot and thymeleaf; I'm trying to do a form where I will list numbers and the user will select them; however I would like to "checked" the third element (when equals 3) and I cannot see that the input is checked.
我在加载信息和显示复选框时没有问题,当我想在页面加载时默认选中其中一个时出现问题.
I don't have problems loading the information and display the checkboxes, the problem came when I want to check one of them by default when the page loads.
我需要一些帮助来确定应该是什么问题,或者它是否是 th:checked 属性的错误.
I need some help identifying what it should be the issue or if it is a bug with th:checked property.
我在控制器中有这个
@ModelAttribute("allFeatures")
public List<Integer> populateFeatures() {
return Arrays.asList(1, 2, 3, 4, 5, 6);
}
这是html代码
<ul>
<li th:each="feat : ${allFeatures}">
<input type="checkbox" th:field="*{features}" th:value="${feat}" th:checked="${feat == 3}"></input>
<label th:for="${#ids.prev('features')}" th:text="${'seedstarter.feature.' + feat}">Electric Heating</label>
</li>
<li th:remove="all">
<input id="removed1" type="checkbox"/> <label for="removed1">Turf</label>
</li>
</ul>
提前致谢.
推荐答案
使用 th:checked
并避免使用 th:field
并包含 'name'代码>属性
Use th:checked
and avoid th:field
and include 'name'
attribute
<input type="checkbox" th:checked="${feat} == 3" name="features"></input>
额外评论
但正确的方法是避免使用 th:checked
和 'name'
属性并坚持使用 th:field
.但是在这种情况下,您必须确保在将其发布到视图之前在控制器中初始化模型属性 "features"
.在这里,您可以定义默认情况下从控制器本身选中哪个复选框.
But the proper way will be to avoid th:checked
and 'name'
attributed and stick to the th:field
. But in that case you have to make sure that you initialize the model attribute "features"
in the controller before posting it to the view. Here you can define which checkbox to get checked by default from the controller itself.
但是您的情况似乎有点棘手,因为您想显示复选框的动态列表.所以我更喜欢第一个解决方案.
But your situation seems a bit tricky as you want to display the dynamic list of checkboxes. So I prefer the first solution.
这篇关于使用带有百里香叶的类型复选框检查输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论