在Javascript中使用jstl [英] using jstl in Javascript

查看:75
本文介绍了在Javascript中使用jstl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的jsp页面中,我有:

in my jsp page I have:

<form:select path="index" id="sIndex" onchange="showDetails()">
    <form:options items="${smth}" itemLabel="name" itemValue="index"/>
</form:select>

在我的javascript函数中:

And in my javascript function:

 *function showDetails() {
        var sIndex=document.getElementById("sIndex");
        var index=sIndex[sIndex.selectedIndex].value;
        var name = '${smth[index].name}';
        var address = '${smth[index].address}';
        var message = "<table><tr><td>Name:</td><td>" + name + "</td></tr>";
        message = message + "<tr><td>Address:</td><td>" + address + "</td></tr>"
        message = message + "</table>"
        document.getElementById("candDetails").innerHTML = message;
    }*

它并不需要$ {}中的索引,但是如果我使用alert(index),它就可以识别它.

And it doesn't takes the index in ${}, but if I use alert(index) it recognize it.

推荐答案

Java/JSP/JSTL在服务器端运行,生成HTML/CSS/JS输出并将其发送给客户端. HTML/CSS/JS在客户端运行,而不是您显然期望的在服务器端运行.在浏览器中打开页面,然后执行查看源代码".你看到了吗?

Java/JSP/JSTL runs at the server side, produces HTML/CSS/JS output and sends it to the client. HTML/CSS/JS runs at the client side, not at the server side as you apparently expected. Open the page in your browser and do a 'view source'. Do you see it?

Javascript只在客户端看到HTML DOM树,并且可以访问它.您需要从HTML DOM树中获取名称和地址.您已经在option元素中拥有名称,但是该地址无处可用.您可以使用JSTL生成Javascript数组变量,以便Javascript代码可以进一步使用它.

Javascript only sees the HTML DOM tree in the client side and can access it. You need to get the name and address from the HTML DOM tree. You already have the name in the option element, but the address is nowhere available. You could use JSTL to generate a Javascript array variable so that the Javascript code can use it further.

要详细了解Java/JSP和Javascript之间的隔wall,您可以找到这篇文章有用.

To learn more about the wall between Java/JSP and Javascript you may find this article useful.

这篇关于在Javascript中使用jstl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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