我对JSP和javascript很新。我使用javascript计算两个地方之间的距离,并将距离存储在文本框中。 document.getElementById('dist').value = drivingDistanceKilometers;
然而,当我尝试使用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>
Inside bookings.jsp:
<body>
<%
String dist = request.getParameter("dist");
out.println(dist);
%>
</body>
答案 0 :(得分:0)
您实际上没有名称为dist的输入字段。你需要做的是修改你的html并将其插入你的表格
<input type="hidden" id="dist" name="dist" value="0"/>
所以你的代码可能如下所示:
<form name="form2" onSubmit=" showLocation(); " method="post" action="bookings.jsp" >
<input type="hidden" id="dist" value="0"/>
<input name="pickUp" type="text">
<input name="dropOff" type="text">
<input type="submit" value="Submit">
</form>