[英]Thymeleaf multiple result in condition
I want to check some condition basing on statement 'myStatus' and if it is true
- change background color to red and disable element (user could not click it), if it is false
- change color to green. 我想根据语句“ myStatus”检查某些条件,如果为
true
,则将背景颜色更改为红色并禁用元素(用户无法单击它),如果为false
,则将颜色更改为绿色。 I know how to change color (and it works fine): 我知道如何更改颜色(效果很好):
th:style="${myObject.myStatus} == true ? 'background-color:red' : 'background-color:green'"
but how is it possile to disable
element in this line? 但是如何
disable
此行中的元素? I tried something like: 我尝试了类似的东西:
th:style="${myObject.myStatus} == true ? 'background-color:red' 'disabled:false' : 'background-color:green'"
th:style="${myObject.myStatus} == true ? 'background-color:red' 'disabled' : 'background-color:green'"
th:style="${myObject.myStatus} == true ? 'background-color:red' & disable : 'background-color:green'"
but it doesnt work... how is it possible to solve this problem? 但是它不起作用...如何解决这个问题?
@UPDATE @更新
@RequestMapping(value = "/getObject/{someID}", method = RequestMethod.GET)
public String getAttr(@PathVariable(value="someID") String id, Model myObject){
myObject.addAttribute("myObject", this.objectCreator.getObjects());
return "/chosenobject";
}
and it works (because colors works fine). 并且可以正常工作(因为颜色可以正常工作)。 I use it in
form
element. 我在
form
元素中使用它。
To disable a HTML element you have to use the HTML attribute disabled
: https://www.w3schools.com/tags/att_disabled.asp . 要禁用HTML元素,您必须使用HTML属性
disabled
: https : //www.w3schools.com/tags/att_disabled.asp 。 You can do this with Thymeleaf as the following: 您可以使用Thymeleaf进行以下操作:
th:disabled="${myObject.myStatus}"
is assume myObject.myStatus
is either a true of false boolean value 假设
myObject.myStatus
是布尔值的true
To set the color conditionally do the following: 要有条件地设置颜色,请执行以下操作:
th:style="${myObject.myStatus} ? 'background-color:red' : 'background-color:green'"
You can use Javascript to disable an element if a condition sent from the controller evaluates to true (or false). 如果从控制器发送的条件评估为true(或false),则可以使用Javascript禁用元素。
document.getElementById("id").disabled = true;
You can also mock the disabled
attribute in CSS with pointer-events:none
like this: 您还可以在CSS中使用
pointer-events:none
模拟disabled
属性pointer-events:none
如下所示:
th:style="${myObject.myStatus} == true ? 'background-color:red;' : 'background-color:green; pointer-events: none;'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.