在 Thymeleaf 中使用 onClick 时如何将参数传递给 javascript 函数调用 [英] How to pass arguments to javascript function call when using onClick in Thymeleaf

查看:22
本文介绍了在 Thymeleaf 中使用 onClick 时如何将参数传递给 javascript 函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直从 Thymeleaf 调用我的 javascript 函数,如下所示:

I have been calling my javascript function from Thymeleaf as below:

th:onclick="'viewDocument(\'' + ${document.docTypeLongDesc} +'\');'"

但我刚刚将 Spring Boot 版本更新为 2.1.4 RELEASE,Thymeleaf 也随之更新.并且不再支持以前的版本.

But I just updated my spring boot version to 2.1.4 RELEASE with which Thymeleaf also got updated. And the previous version in no longer supported.

进一步研究我发现我应该能够使用

On further research I found out that I should be able to use

th:onclick="' viewDocument (this.getAttribute ('document.docTypeLongDesc'));'"

然而,它没有给出任何错误,但它也不起作用.我已经删除了参数并且能够很好地调用该函数.所以我猜我没有以正确的方式传递论点.任何指导都会有所帮助.TIA.

However, it doesn't give any error but neither does it work. I have removed the argument and was able to call the function just fine. So I am guessing I am not passing the argument right way. Any guidance will be helpful. TIA.

推荐答案

参见:受限模式: 避免变量表达式在事件处理程序的处理器中返回字符串 (th:on*).

为了正确地将 Thymeleaf 变量传递给 onclick 事件,请将变量放在 data 属性中,并使用 getAttribute() 读取它.

In order to correctly pass Thymeleaf variables to the onclick event, put the variable in a data attribute, and read it using getAttribute().

th:data-longDescription="${document.docTypeLongDesc}" onclick="viewDocument(this.getAttribute('data-longDescription'));"

这篇关于在 Thymeleaf 中使用 onClick 时如何将参数传递给 javascript 函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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