<script type="text/javascript">
        function test(){
            var s =document.getElementById("Lx").value;
        }
    </script>
  </head>
  
  <body>
    <select name="Lx" id="Lx"> 
    <option value="1">1 </option> 
    <option value="2">2 </option> 
    <option value="3">3 </option> 
</select> 
<input type="submit" id="submit" name="submit" value="" onclick="test()">
  </body>
<%
String b;
%>
刚才sooneasy朋友帮我解决了一个问题,接下来又有新问题了。以上是实现一点submit就把当前值赋给变量s但是我想把s的值再赋给当前页面的jsp变量b那要如何做呢?

解决方案 »

  1.   

    没法做
    其实可以ajax提交到后台去处理,然后在当前页面拿出来。
    貌似这样就太复杂了
      

  2.   

    LZ这个问题以前问过吧
    还是那个答案:在同一个页面中 js的变量值不能给jsp中的变量赋值.
    因为js是下载到客户机上执行,而jsp是在服务器端执行,在服务器端执行完了以后,转换为html代码发送给客户端,也就是说用户在客户端看到的是html代码,所以jsp可以给js赋值,但js是永远也不能给jsp赋值。
    你想把s的值再赋给当前页面的jsp变量b,那你只能把s的值发给当前页面的jsp文件,再在这个jsp文件里给变量b赋值,重新生成当前页面,例如:
    index.jsp<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>index.jsp</title>
    <script type="text/javascript">    
                var s ; 
                function test() {
                   s =document.getElementById("Lx").value; 
                   window.location.href='index.jsp?s='+s;
                  } 
              </script>
    </head>
    <body>
    <select id="Lx">
    <option value="1">
    1
    </option>
    <option value="2">
    2
    </option>
    <option value="3">
    3
    </option>
    </select>
    <input type="submit" value="submit" onclick="test()" />
    <br />
    jsp: s=
    <%
    String s = (String) request.getParameter("s");
    if (s != null && !"".equals(s)) {
    out.print(s);
    }
    %>
    </body>
    </html>