运行 JS 函数后,我需要更改表单中隐藏类型的值。距离的值似乎是正确的。但是当我在下一页访问 $_POST['distance'] 时,分配似乎不起作用,它仍然返回 1000 而不会更改值。请帮忙。
<script type="text/javascript">
function calcRoute() {
var start = "1255 E University Dr, Tempe, AZ 85281";
var end = "KPMG, 60 E Rio Salado Pkwy, Tempe, AZ 85281";
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
window.distance = result.routes[0].legs[0].distance.value;
alert(window.distance);
directionsDisplay.setDirections(result);
}
});
document().getElementById('distance').value = window.distance;
}
</script>
<button type="button" class="btn" onclick="calcRoute()">calc</button>
<form method="post" action = "getdist.php">
<input type = "hidden" name="distance" id="distance" value="1000">
<input type="submit" value="Submit">
</form>