在不使用th:inline的情况下将变量表达到javascript中 [英] Variable expression into javascript without using th:inline

查看:48
本文介绍了在不使用th:inline的情况下将变量表达到javascript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我先搜索了一下,但发现答案令人困惑,因为我是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} + '\');'"

感谢来到百里香论坛: http://forum.thymeleaf.org/variable-expression-into-javascript-without-using-th-inline-td4025534.html

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屋!

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