我是一个JavaScript的新手(或任何形式的网页开发,真的),并无法追查一个错误。基本上按下“路由(桌面版本)”按钮here应该触发一个函数calcRoutePartOne(),它可以执行此操作。JavaScript刷新页面,我不知道为什么
function calcRoutePartOne()
{
alert("calcRoutePartOne");
shortestTimeIndex = "undefined";
lowestTotalCost = "undefined";
shortestDistanceIndex = "undefined";
shortestDistance = "undefined";
alert("Alert B");
displayLoadingImage();
alert("Alert C");
var startText = document.getElementById("start").value;
alert(startText);
geocoder.geocode(
{
'address':startText
},
function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
start = results[0].geometry.location;
alert(start);
calcRoutePartTwo();
}
else
{
alert("Failed to geocode start address \n Error: " + status);
hideLoadingImage();
}
});
}
此函数执行对我很好,所有的警报发射了,他们应该,但后来不是调用calcRoutePartTwo()的页面只是刷新[编辑:现在,它有时前往calcRoutePartTwo,然后刷新,这也不是它应该做的]。
特别令人费解的是,我之前有过这样的代码工作 - 目前我只是重构,并在UI上工作,所以我不明白这是如何突然开始发生的。
我遇到问题的代码有here。我也有以前的工作代码,这是几乎相同除了UI,here
,因为我开始使用jQuery的问题才开始,所以我不知道是否有一些错误的jQuery导致这种行为?任何时候页面都不应该刷新 - 如代码所示,在到达目的地后它应该重定向到google
谢谢。
编辑:这是可能违规的功能:
function calcRoutePartTwo()
{
alert("calcRoutePartTwo");
var endText = document.getElementById("end").value;
geocoder.geocode(
{
address: endText
},
function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
end = results[0].geometry.location;
console.log(end);
calcRoutePartThree();
}
else
{
alert("Failed to geocode end address \n Error: " + status);
hideLoadingImage();
}
});
}
或者问题可能,我想,是几乎所有地方都在页面上无论是。我只是不知道。
为了解决这个问题,在代码中加入'$('form')。submit(function(e){e.preventDefault()})'。 – 2012-03-17 09:54:49
GAH!当然就是这样。现在很明显,我明白了!谢谢! – 2012-03-17 09:56:16
@Diarmund:因为你是新来的stackoverflow,不要忘记接受一个复选标记的答案:) – Amberlamps 2012-03-17 10:01:28