Thymeleaf - 如何有条件地添加检查属性到输入 [英] Thymeleaf - How to add checked attribute to input conditionally
问题描述
input
组件有一个属性,被选中
是否将复选框标记为默认启用或不。 < input type =checkboxname =mycheckboxchecked =checked/>
默认情况下禁用复选框,选中
应声明异常。是否可以通过Thymeleaf中的一个标记设置 checked
属性?
根据官方的thymeleaf文件
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#fixed-value-boolean-attributes
th:checked
被视为一个固定值布尔属性。
< input type =checkboxname =activeth:checked =$ {user.active}/>
其中 user.active
应该是 boolean
。
所以在你的情况下,应该像 Andrea 提到的那样,
< input type =checkboxname =mycheckboxth:checked =$ {flag}/>
As you know, input
component has an attribute, checked
to whether mark the checkbox as enabled by default or not.
<input type="checkbox" name="mycheckbox" checked="checked"/>
To disable the checkbox by default, the checked
exception should be declared. Is it possible to set checked
attribute by a flag in Thymeleaf?
According to the official thymeleaf documentation
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#fixed-value-boolean-attributes
th:checked
is considered as a fixed-value Boolean attribute.
<input type="checkbox" name="active" th:checked="${user.active}" />
Where user.active
should be a boolean
.
So in your case it should be as Andrea mentioned,
<input type="checkbox" name="mycheckbox" th:checked="${flag}" />
这篇关于Thymeleaf - 如何有条件地添加检查属性到输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!