如何在同一页面的jsp中访问javascript变量?

时间:2014-06-06 12:28:24

标签: javascript jsp

table.jsp

        <script type="application/javascript">
        var ad="image/a11.png";
        var i=0;
        var j;
            function changeImage(a){
        j=document.getElementById(a);


           if(i===0){
          alert("click ok to seat");

      j.value=a;
      alert("j= "+j);
              document.getElementById(a).src = ad;
           //trying to access div with id 'seat'
             window.location.href='#seat?seat='+a;//won't works
             }
             i=i+1;
             }
           </script>
      </head>

      <body>
          <div id="frame">
        <img src="image/seatf.png"  id="1" onclick="changeImage('1')" title="Seat1"  />
     <div style="width:300;height:200;background-color:gray;" id="seat">
    <% 
    if(request.getParameter("seat")!=null){%>
    <h2> value is: <%= request.getParameter("seat") %> </h2>
    <% }%>
    </div>

    </div>

我无法获得参数&#39;座位&#39;的价值。是否是实现此目的的正确方法?
如果没有,那么我怎样才能获得参数&#39;座位的值?#谢谢..

1 个答案:

答案 0 :(得分:0)

这接近你想要完成的目标吗?

<script>
var ad="image/a11.png";
var i=0;
var j;
function changeImage(j){
console.log(document.getElementById('seat').getElementsByTagName('h2')[0].innerHTML);
    if(i===0){
        document.getElementById(j.id).src = ad;
        document.getElementById('seat').getElementsByTagName('h2')[0].innerHTML = 'value is: ' + j.id;
    }
    i+=1;
}
</script>

<div id="frame">
    <img src="image/seatf.png"  id="1" onclick="changeImage(this)" title="Seat1"  />
    <div style="width:300;height:200;background-color:gray;" id="seat">
        <h2> value is: ? </h2>
    </div>

</div>

jsfiddle:http://jsfiddle.net/BloodyKnuckles/w7NBa/