Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

jquery - Javascript function call with Thymeleaf

I need to call a javascript function on thymeleaf template, something like this:

Case 1:

<select th:onclick="${'function1('a')'}">

But in this case the thymeleaf not work.. some researchs ago (including stackoverflow) I get the followings "solutions":

Case 2:

<select th:onclick="${'function1(''a'')'}">

Case 3:

<select th:onclick="${'function1('a')'}">

Case 4:

<select th:onclick="${'function1(''+'a'+'')'}">

But in all cases I get the same error: "...Exception evaluating SpringEL expression..."

My problem is about javascript callings, I need put some parameters ${var} for call in js function. How I can fix that ?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you don't need any dynamic vars in the JS function call, this is how to do it:

th:onclick="'alert('a');'"

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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...