在不使用th:inline的情况下将变量表达到javascript中 [英] Variable expression into javascript without using th:inline
问题描述
我先搜索了一下,但发现答案令人困惑,因为我是Thymeleaf的新手,并且对JavaScript充其量只是业余爱好.
I searched first but I found confusing answers since I'm new to Thymeleaf and amateurish at best at javascript.
我只想知道如何将变量表达式传递到javascript函数中,就像在JSP中那样:
I just want to know how to pass variable expressions into javascript functions, sort of like in JSP:
<a href="#" onclick="javascript:getContactId('${contact.id}');">Button</a>
当然,这不会以Thymeleaf失败,并传递字符串$ {contact.id}而不是其值,那么我怎么能获得变量表达式的值呢?
Of course, this fails with Thymeleaf and passes the string ${contact.id} instead of its value, so how could I get the value of the variable expression instead?
我想要这种方式的原因是因为它取决于被 th:each
迭代的行.
The reason I want it this way is because it depends on the row which is being iterated by th:each
.
如果除了使用 th:inline
外别无其他方法,那么考虑上述声明的最佳方法是什么?
If there's no other way except to use th:inline
, then what's the best approach considering the above statement?
推荐答案
此方法有效:
th:onclick="'javascript:getContactId(\'' + ${contact.id} + '\');'"
Thanks goes out to the thymeleaf forum: http://forum.thymeleaf.org/variable-expression-into-javascript-without-using-th-inline-td4025534.html
这篇关于在不使用th:inline的情况下将变量表达到javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!