使用带有百里香叶的类型复选框检查输入

编程入门 行业动态 更新时间:2024-10-25 01:29:48
本文介绍了使用带有百里香叶的类型复选框检查输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 14:22:11,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百里   复选框   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!