在THymeleaf中使用onClick时如何将参数传递给javascript函数调用

时间:2019-04-17 11:19:38

标签: java spring-boot thymeleaf

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

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

但是我刚刚将Spring Boot版本更新为2.1.4 RELEASE,Thymeleaf也通过该版本进行了更新。并且不再支持以前的版本。

在进一步研究中,我发现我应该可以使用

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

但是,它没有给出任何错误,但也不起作用。我删除了参数,并能够很好地调用该函数。所以我想我没有正确地传递论点。任何指导都会有所帮助。 TIA。

2 个答案:

答案 0 :(得分:0)

请参见:Restricted mode: Avoid variable expressions returning strings in processors for event handlers (th:on*)

为了正确地将Thymeleaf变量传递给onclick事件,请将变量放在data属性中,然后使用getAttribute()进行读取。

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

答案 1 :(得分:-1)

您应按以下方式使用它:

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