gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:37:55 25 4
gpt4 key购买 nike

第 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com