【问题标题】:Thymeleaf checkbox can't default to checked when using th:field?Thymeleaf 复选框在使用 th:field 时不能默认选中?
【发布时间】:2019-03-22 17:10:02
【问题描述】:

有谁知道如何在 Thymeleaf、SpringBoot、Java 项目中将默认设置 checked 添加到单个复选框的输入字段中? 我必须使用th:field,因为我稍后会通过电子邮件提交此表单,如果我使用th:name 并默认选中复选框,则默认值有效,但电子邮件无效。

有什么建议吗?

<label>
       <input type="checkbox" id="serviceLevel"  th:field="*{serviceLevel}" th:checked="${serviceLevel}"/>
Affiliate Serviced</label>

通过电子邮件提取价值的电子邮件 html 代码:

<tr class="emailRow">
  <h3>Direct: </h3>
    <td class="bodycopy" th:text="${directBind.directBox}">
    </td>
</tr>

我在模型中将此变量设置为私有布尔直接框。

我已经尝试了所有方法:th:checked=checkedvalue=truevalue=checked、设置值和checked ="${directBox}" 这些都不起作用。

有解决办法吗?

【问题讨论】:

  • 如果你想使用th:field,你可能需要改变你的控制器。看到这个线程:forum.thymeleaf.org/…
  • 你知道如果我不使用 th:field (和 th:name 代替),我怎样才能让 th:text 通过电子邮件发送值?我所有的其他表单元素都是 th:field,在电子邮件助手上,我将其作为 th:text 来读取每个输入的答案。

标签: java spring-boot thymeleaf


【解决方案1】:

想出了一种使用 th:field 的方法,并将其默认选中。必须在我的控制器中设置它,就像 Gustavo 提到的那样。下面的代码示例。

...
directBind.setDirectBox(true);
directBind.setServiceLevel(true);
model.addAttribute("directBind", directBind);
return "directBind";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多