如何在 JavaScript/jQuery 函数中使用表达式语言 (EL)?
如何从 JSP 中的 表达式语言 (EL) 获取值到 JavaScript 函数?
在我的 JSP 页面中,我有:
<li>${state}</li>
<input title="Show ${state}">Show</>
我希望能够将 ${state}
从 JSP 获取到 JavaScript 函数中:
$('#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}">");
}
});
我希望每个按钮的标题都显示“显示俄亥俄州”或“隐藏”俄亥俄州,但要更改
标记中的任何州。How do you get a value from Expression Language (EL) in a JSP to a JavaScript function?
In my JSP page I have:
<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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 必须放入
.jsp
文件中的中,而不是
.js
文件中,以便使 JSP 内联EL 变量。或者,您也可以仅对第一个单词或前四个字符的子字符串进行简单的字符串替换。这样你就不需要 JavaScript 中的任何 EL,因此你可以将 JS 代码放在它自己的
.js
文件中。顺便说一句,整个函数的逻辑流程本身就没有什么意义。难道你真的不想检查
val()
是否是“隐藏”
吗?您实际上不想在两个条件之一的标题中使用"Show"
吗?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?如果您可能的值是
Show ${State}
和Hide ${State}
那么您实际上只关心前四个字符:If your possible values are going to be
Show ${State}
andHide ${State}
then you really only care about the first four characters: