using jstl in Javascript - Stack Overflow

in my jsp page I have:<form:select path="index" id="sIndex" onchange="showD

in my jsp page I have:

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

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;
    }*

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

in my jsp page I have:

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

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;
    }*

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

Share Improve this question edited Feb 12, 2014 at 12:14 BenMorel 36.7k51 gold badges205 silver badges336 bronze badges asked Nov 2, 2009 at 22:21 user198298user198298 111 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

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

The EL expressions (the code between ${}) are evaluated at runtime of the JSP servlet, not once the page has been rendered in the browser, which is when your JavaScript is being called.

View the generated source of the page and you will probably see the problem.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742383891a4433662.html

相关推荐

  • using jstl in Javascript - Stack Overflow

    in my jsp page I have:<form:select path="index" id="sIndex" onchange="showD

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信