Thymeleaf - 如何有条件地添加检查属性到输入 [英] Thymeleaf - How to add checked attribute to input conditionally

查看:1652
本文介绍了Thymeleaf - 如何有条件地添加检查属性到输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你所知道的, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆