我一直在调用Thymeleaf的javascript函数,如下所示:
th:onclick="'viewDocument(\''
+${document.docTypeLongDesc} +'\');'"
但我刚刚将我的春季启动版本更新为2.1.4 RELEASE,Thymeleaf也更新了。并且不再支持以前的版本。
在进一步的研究中,我发现我应该可以使用
th:onclick="' viewDocument (this.getAttribute ('document.docTypeLongDesc'));'"
但是,它不会产生任何错误,但也不起作用。我删除了参数,并能够正常调用该函数。所以我猜我没有正确地传递论据。任何指导都会有所帮助。 TIA。
为了将Thymeleaf变量正确传递给onclick事件,将变量放在data
属性中,并使用getAttribute()
读取它。
th:data-longDescription="${document.docTypeLongDesc}" onclick="viewDocument(this.getAttribute('data-longDescription'));"
您应该按如下方式使用它:
th:onclick="${'viewDocument(' + document.docTypeLongDesc + ');'}"