开发者

How to use Expression Language (EL) in a JavaScript/jQuery function?

开发者 https://www.devze.com 2023-04-10 03:38 出处:网络
How do you get a value from Expression Language (EL) in a JSP to a JavaScript function? In my JSP page I hav开发者_Go百科e:

How do you get a value from Expression Language (EL) in a JSP to a JavaScript function?

In my JSP page I hav开发者_Go百科e:

<li>${state}</li>
<input title="Show ${state}">Show</>

And I would like to be able to get the ${state} from the JSP into the JavaScript function:

$('#input').click(function() { 
if ($(this).val() == "Show + ${state}">") 
{ 
    $(this).val("Show"); 
    $(this).attr("title", "Hide + ${state}"> "); 
} 
else 
{ 
    $(this).val("Hide"); 
    $(this).attr("title", "Hide + ${state}">"); 
} 
}); 

I want for the title of each button to say show Ohio or Hide Ohio, but to change for whatever state is in the <li> tag.


That JavaScript has to go in a <script> inside a .jsp file instead of a .js file in order to get JSP to inline the EL variables.

Alternatively, you can also just do a simple string-replace of the first word or a substring of the first four characters. This way you don't need any EL in the JavaScript and thus you can just put the JS code in its own .js file.

By the way, the logic flow of your whole function makes at its own very little sense. Don't you actually want to check if the val() is "Hide"? Don't you actually want to use "Show" in the title of one of the two conditions?


If your possible values are going to be Show ${State} and Hide ${State} then you really only care about the first four characters:

if ($(this).val().substring(0, 4) == "Show")
{
    ...
}
...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号