對於JSP和javascript來說,這是相當新穎的。我使用JavaScript計算兩個地方之間的距離並將距離存儲在文本框中。 document.getElementById('dist').value = drivingDistanceKilometers;
將一個javascript中設置的參數從一個jsp發送到另一個jsp
但是,當我嘗試使用String dist = request.getParameter("dist");
檢索值時,我沒有收到任何價值。
另外我嘗試使用document.location.href = "bookings.jsp?dist="+drivingDistanceKilometers;
這是第一次工作,但當我再試一次,它沒有價值。
我該如何解決這個問題?
共享更多的代碼:
這裏面book.jsp
的Javascript:
<script>
var geocoder, location1, location2, gDir;
function initialize() {
geocoder = new GClientGeocoder();
gDir = new GDirections();
}
function showLocation() {
geocoder.getLocations(document.form2.pickUp.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(document.form2.dropOff.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
calculateDistance();
}
});
}
});
}
function calculateDistance()
{
GEvent.addListener(gDir, "load", function() {
var drivingDistanceKilometers = gDir.getDistance().meters/1000;
document.getElementById('dist').value = drivingDistanceKilometers;
});
}
</script>
HTML:
<body onload="initialize()">
<form name="form2" onSubmit=" showLocation(); " method="post" action="bookings.jsp" >
<input type="text" id="dist" name="dist" value="0" >
<input name="pickUp" type="text">
<input name="dropOff" type="text">
<input type="submit" value="Submit">
</form>
</body>
內bookings.jsp:
<body>
<%
String dist = request.getParameter("dist");
out.println(dist);
%>
</body>
你確定你有一個值'drivingDistanceKilometers' – Tariq 2014-09-24 12:30:46
@Tariq是的。如果我沒有表單操作,那麼距離值出現在文本框中,當我點擊我的提交按鈕。 – Sachin 2014-09-24 12:39:02
實際上使用普通的JSP。與HTML或PHP頁面相比,完全沒有區別。你需要查看你的代碼。或者更好的分享一些代碼,讓我們更好地瞭解問題 – Tariq 2014-09-24 13:08:24