用Thymeleaf进行Javascript函数调用 [英] Javascript function call with Thymeleaf
问题描述
我需要在thymeleaf模板上调用javascript函数,如下所示:
I need to call a javascript function on thymeleaf template, something like this:
情况1:
<select th:onclick="${'function1('a')'}">
但是在这种情况下,百里香无法工作..一些研究(包括stackoverflow)之前,我得到以下解决方案":
But in this case the thymeleaf not work.. some researchs ago (including stackoverflow) I get the followings "solutions":
情况2:
<select th:onclick="${'function1(''a'')'}">
情况3:
<select th:onclick="${'function1(\'a\')'}">
情况4:
<select th:onclick="${'function1(\''+'a'+'\')'}">
但是在所有情况下,我都会遇到相同的错误:"...评估SpringEL表达式的异常..."
But in all cases I get the same error: "...Exception evaluating SpringEL expression..."
我的问题是关于javascript调用的,我需要在js函数中放置一些参数$ {var}来进行调用.我该如何解决?
My problem is about javascript callings, I need put some parameters ${var} for call in js function. How I can fix that ?
谢谢
推荐答案
如果您在JS函数调用中不需要任何动态变量,这是这样做的方法:
If you don't need any dynamic vars in the JS function call, this is how to do it:
th:onclick="'alert(\'a\');'"
这只是转义单引号,不需要SpringEL(当然,在这种情况下,您可以省去thymeleaf属性,而只需使用普通的onclick).
This simply escapes the single quotes and requires no SpringEL (of course you could dispense with the thymeleaf attribute in this case and just use plain onclick).
要在其中插入vars:
To insert vars into it:
th:onclick="'alert(\'' + ${myVar} + '\');'"
使用警报功能让我尝试一下并证明它有效.希望有帮助.
Used the alert function to allow me to try it out and prove it works. Hope that helps.
这篇关于用Thymeleaf进行Javascript函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!