JSP与JS在两个不同业面怎么传递值,请给我个例子好吗

解决方案 »

  1.   

    在二个页面间先由JSP完成传值,再由JSP给值JS.
      

  2.   

    a.jsp<a href="b.jsp?name=xxx>Click to b.jsp</a>
    b.jsp
    <%
    String name = request.getParameter("name");
    %>
    <script>
    var name = "<%=name%>" ;
    alert("hello "+name) ;
    </script>
      

  3.   

    JSP页面<%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.util.*" %> <html> 
      <body> 
      <%
       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
       String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Cstar";    String user="sa"; 
    String password="sa";
    Connection conn= DriverManager.getConnection(url,user,password);
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
    String sql="select * from Building";
    ResultSet rs=stmt.executeQuery(sql);%>
    <%  while(rs.next()){ %>
    <TR>
          <FORM ACTION="Query.jsp" METHOD="POST" name="Query">
    <INPUT TYPE="button" name="button1" value="test" onclick="getValue();">
    <input type="hidden" name="firstFiled" id="firstFiled" value="<%=rs.getString(1)%>">
    <input type="hidden" name="secondFiled" id="secondFiled" value="<%=rs.getString(2)%>"> </TR>
    <%  }%>      </FORM>
      </body> 
      </html>
    <script>
    function getValue(){
    var firstFiled = document.getElementById("firstFiled").value;
    var secondFiled = document.getElementById("secondFiled").value;
    alert(firstFiled);
    alert(secondFiled);

    }
    </script>JS页面Win1.document.writeln('<Query.jsp?method=" + firstFiled ">');
      

  4.   

    调用JS的函数,由jsp页面将值作为参数传给js中的函数~
      

  5.   

    首先<FORM ACTION="Query.jsp" METHOD="POST" name="Query"> 应该在while循环外部其次,你的hidden input可能有多个(因为循环取数据),这样你取到的var firstFiled = document.getElementById("firstFiled").value;是不确定的值,按照js,应该取的是第一条,不知道是不是你要的这个意图
      

  6.   

    你需要对hidden input加上id的序号但随之的问题,你通过一个button来遍历,可能要在function中写一个循环了
      

  7.   

    在while之前加一个计数器
    int counter = 0;while(rs.next())
    {
    couter++ ; //计数器加1......
    ......
    <input type="hidden" name="firstFiled" id="firstFiled<%=counter%>" value="<%=rs.getString(1)%>">
    <input type="hidden" name="secondFiled" id="secondFiled<%=counter%>" value="<%=rs.getString(2)%>">
    }<script>
    function getValue(){
    var counter = "<%=counter%>" ;
    for(var i=1; i<counter;i++)
    {
    var firstFiled = document.getElementById("firstFiled"+counter).value;
    var secondFiled = document.getElementById("secondFiled"+counter).value;
    alert("第"+counter+"条记录的第一个值:"+firstFiled);
    alert("第"+counter+"条记录的第二个值:"+secondFiled);
    }
    }}
    </script>