文本字段值不会传递到另一个页面

时间:2014-08-23 19:34:22

标签: javascript html jsp

Page 1

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body onload="geolocation()">

        <form method="GET" action="newjsp2.jsp">

         <input type ="text" id="loc1" name ="loc1" />
        <input type ="text" id ="lonandlat1" name ="lonandlat1"/>
            <input type="submit" value="submit">
        </form>
    </body>
    <script>
          function geolocation(){



      function displayLocation(latitude,longitude){
        var request = new XMLHttpRequest();

        var method = 'GET';
        var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
        var async = true;

        request.open(method, url, async);
        request.onreadystatechange = function(){
          if(request.readyState == 4 && request.status == 200){
            var data = JSON.parse(request.responseText);
            var address = data.results[0];
           // document.write(address.formatted_address);
             document.getElementById("loc1").value = address.formatted_address;
         //    alert(document.getElementById("loc1").value);
          }
        };
        request.send();
      };

      var successCallback = function(position){
        var x = position.coords.latitude;
        var y = position.coords.longitude;
        document.getElementById("lonandlat1").value=x+" "+y;
        displayLocation(x,y);
      };

      var errorCallback = function(error){
        var errorMessage = 'Unknown error';
        switch(error.code) {
          case 1:
            errorMessage = 'Permission denied';
            break;
          case 2:
            errorMessage = 'Position unavailable';
            break;
          case 3:
            errorMessage = 'Timeout';
            break;
        }
        document.write(errorMessage);
      };

      var options = {
        enableHighAccuracy: true,
        timeout: 10000,
        maximumAge: 6000
      };
       navigator.geolocation.getCurrentPosition(successCallback,errorCallback,options);


}

    </script>

</html>

第2页

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <input type ="text" id="loc1" value="<%  request.getParameter("loc1"); %>"/>

        <input type ="text" id="lonandlat1" value ="<%request.getParameter("lonandlat1");%>" />
    </body>
</html>

这是我项目的非常小的代码。在第1页中,我首先获取处理 javascript getlocation()功能的当前位置,然后将其调入 onload 事件获取后的第一页我将这些值传递给下一页第2页,但我不知道为什么值没有传递给第2页

感谢。

3 个答案:

答案 0 :(得分:2)

首先,通过在 page2.jsp 中编写的所有代码上方的响应获取'lonandlat1'。

        <%String var = request.getParameter("lonandlat1");%>

然后替换:

        <input type ='text' id='lonandlat1' value ='<%request.getParameter('lonandlat1');%>' /> 

通过

       <input type ='text' id='lonandlat1' value ='<%=var%>' />  

类似的东西:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%

    String var = request.getParameter("lonandlat1");
    String var2 = request.getParameter("loc1");

    %>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>

            <input type ="text" id="loc1" value="<%=var2%>"/>

            <input type ="text" id="lonandlat1" value ="<%=var%>" />
        </body>
    </html> 

答案 1 :(得分:1)

试试这个:

<%!
 String s;
%>

<%
s= request.getParameter("lonandlat1");
%>

<input type ="text" id="lonandlat" value ="<%out.print(s);%>" />

答案 2 :(得分:0)

我认为您应该将您的第一页划分为两个,您需要通过在地理位置中单击提交输入数据。您加载它,这意味着它将在此页面加载后立即调用。

我不是专家,但对我来说似乎有问题。