Html Thymeleaf - 如何将选中的属性添加到有条件的输入中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29826576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:54:06  来源:igfitidea点击:

Thymeleaf - How to add checked attribute to input conditionally

htmlthymeleafhtml-input

提问by mtyurt

As you know, inputcomponent has an attribute, checkedto whether mark the checkbox as enabled by default or not.

如您所知,input组件有一个属性,checked用于是否将复选框标记为默认启用。

<input type="checkbox" name="mycheckbox" checked="checked"/>

To disable the checkbox by default, the checkedexception should be declared. Is it possible to set checkedattribute by a flag in Thymeleaf?

要默认禁用复选框,checked应声明异常。是否可以checked通过 Thymeleaf 中的标志设置属性?

回答by Faraj Farook

According to the official thymeleaf documentation

根据 thymeleaf 官方文档

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#fixed-value-boolean-attributes

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#fixed-value-boolean-attributes

th:checkedis considered as a fixed-value Boolean attribute.

th:checked被视为固定值布尔属性。

<input type="checkbox" name="active" th:checked="${user.active}" />

Where user.activeshould be a boolean.

哪里user.active应该是boolean.

So in your case it should be as Andreamentioned,

所以在你的情况下,应该像安德里亚提到的那样,

<input type="checkbox" name="mycheckbox" th:checked="${flag}" />

回答by mtyurt

After digging a little, I found out the solution. There is th:checkedattribute for that purpose.

经过一番挖掘,我找到了解决方案。th:checked为此目的有属性。

This works:

这有效:

<input type="checkbox" name="mycheckbox" th:checked="${flag} ? 'checked'">

This fails:

这失败了:

<input type="checkbox" name="mycheckbox" th:checked="${flag} ? 'checked' : ''">

If checked=""is set to inputcomponent, it's marked checked. This method is valid for custom attributesth:attralso. Consider following example:

如果checked=""设置为input组件,则标记为选中。此方法也适用于自定义属性th:attr。考虑以下示例:

<p th:attr="customattr=${flag}?'attr'></p>

If flagis true, it's replaced with:

如果flag为真,则替换为:

<p customattr="attr"></p>

If flagis false, it's replaced with:

如果flag为假,则替换为:

<p></p>

回答by Ameera Najah Kv

You can conditionally add checked attribute to radio input in thymeleaf as below:

您可以有条件地将选中的属性添加到百里香叶中的无线电输入,如下所示:

 <input type="radio" th:checked="${sales.sales_head.sales_type} == CREDIT" class="sales_type" value="CREDIT"  name="sales_type" >

Here if sales_type is CREDIT the radio will be checked. Otherwise it remains unchecked.

在这里,如果 sales_type 是 CREDIT,将检查收音机。否则它保持未选中状态。

回答by redochka

Neither suggested solutions worked for me.

没有建议的解决方案对我有用。

This one worked:

这个有效:

th:checked="${#strings.equals(param.myRequestParameterXYZ, 'foobar')}"

回答by Abdur Rahman

I faced problem for showing checkbox (tick mark on/off) in thymeleaf based on true or false value for the property. I solved it by following way.

我遇到了根据属性的 true 或 false 值在 thymeleaf 中显示复选框(勾选标记开/关)的问题。我通过以下方式解决了它。

Method in controller

控制器中的方法

@RequestMapping(value = "/test")
public String showCheckbox(Model model) {
 boolean myBooleanVariable = false;
 model.addAttribute("myBooleanVariable", myBooleanVariable);
 return "sample-checkbox";
}

In HTML page:

在 HTML 页面中:

<input type="checkbox" name="myBooleanVariable" th:checked="${myBooleanVariable}"/>