Javascript 在 onclick 属性中使用 thymeleaf 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32650536/
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
Using thymeleaf variable in onclick attribute
提问by Kleber Mota
In my current spring-boot project, I have one view with this html code:
在我当前的 spring-boot 项目中,我有一个带有此 html 代码的视图:
<button type="button" class="btn btn-primary" onclick="upload()" th:utext="#{modal.save}"></button>
in the onclick
attribute, the call for the function upload()
should have one parameter, which value is stored in the thymeleaf variable ${gallery}
.
在onclick
属性中,对函数的调用upload()
应该有一个参数,该值存储在 thymeleaf 变量中${gallery}
。
Anyone can tell mehow to use the expression in the above command?
谁能告诉我如何使用上述命令中的表达式?
I already try this:
我已经尝试过这个:
th:onclick="upload(${gallery)"
th:attr="onclick=upload(${gallery)"
th:onclick="upload(${gallery)"
th:attr="onclick=upload(${gallery)"
None of this worked.
这些都没有奏效。
回答by Kleber Mota
I solve this issue with this approach:
我用这种方法解决了这个问题:
th:onclick="|upload('${command['class'].simpleName}', '${gallery}')|"
回答by LiaNg
thymeleaf 3.0.10 th:onclick thymeleaf variable not working
thymeleaf 3.0.10 th:onclick thymeleaf 变量不起作用
This should work:
这应该有效:
th:attr="onclick=|upload('${gallery}')|"
回答by snw
This should work:
这应该有效:
<button th:onclick="'javascript:upload(' + ${gallery} + ')'"></button>
回答by Leon
The older replies does not work in the new version of 3.0.10. Now you need:
旧的回复在 3.0.10 的新版本中不起作用。现在你需要:
<button th:data-id="${quartzInfo.id}"
onclick="del(this,this.getAttribute('data-id'))" type="button">
</button>
回答by Eric ABOUDARAM
In 3.0.10 version of Thymeleaf
在 Thymeleaf 3.0.10 版本中
use [[ ]] it's more easier , thymeleaf goes to inject the value in template
使用 [[]] 更容易,thymeleaf 去注入模板中的值
if the value is string no quote need
如果值是字符串,则不需要引号
<button th:data-id="${quartzInfo.id}"
th:onclick="del(this,[[${quartzInfo.id}]]" type="button">
</button>
回答by paradisa
yes it's to late, but i hope this will help people who need it
是的,为时已晚,但我希望这会帮助需要它的人
try this
尝试这个
th:onclick="'upload('+${gallery}+')'"