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

问题描述 投票:0回答:2

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

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

但我刚刚将我的春季启动版本更新为2.1.4 RELEASE,Thymeleaf也更新了。并且不再支持以前的版本。

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

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

但是,它不会产生任何错误,但也不起作用。我删除了参数,并能够正常调用该函数。所以我猜我没有正确地传递论据。任何指导都会有所帮助。 TIA。

java spring-boot thymeleaf
2个回答
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
投票

您应该按如下方式使用它:

th:onclick="${'viewDocument(' + document.docTypeLongDesc + ');'}"
© www.soinside.com 2019 - 2024. All rights reserved.