javascript - 文本字段值未传递到另一个页面

标签 javascript html jsp

第 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 页 中,我首先获取在 javascriptgetlocation() 函数中处理的当前位置,并将其调用到 onload 第一页的事件,然后在获取后我将这些值传递给下一页,即 第 2 页,但我不知道为什么值没有传递给 第 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> 

关于javascript - 文本字段值未传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465692/

相关文章:

javascript - 如何创建 'dot function' ? (js 对象?)

xml - 在jsp中显示xml

jsp - 赋予空值的参数标签

html - 使用 CSS 添加边距

java - 如何解决JSP中Oracle缺少表达式的问题?

javascript - 从狭窄的输入中仅获取文本值的实际可见部分(获取输入 "scroll")

javascript - Ant Design/antd 日历在模态内不起作用

javascript - 在 React.js 中,我应该在 componentWillMount 还是 componentDidMount 中发出我的初始网络请求?

javascript - 在 p5 dom 上单击后如何删除提交按钮

javascript - CSS3 中的页面 curl (翻转)