Javascript局部和全局变量在回调函数中失去作用域
我尝试过不同的变量作用域,但似乎没有一个有效?我的回调得到了一个有效的结果,但无论我分配给它的变量的范围如何,一旦回调结束,我就会丢失该值Javascript局部和全局变量在回调函数中失去作用域,javascript,Javascript,我尝试过不同的变量作用域,但似乎没有一个有效?我的回调得到了一个有效的结果,但无论我分配给它的变量的范围如何,一旦回调结束,我就会丢失该值 var geocoder; var Lat; var Long; function codeAddress() { var geocoder = new google.maps.Geocoder(); var addy1...... geocoder.geocode({ 'address': fullAddress }, f
var geocoder;
var Lat;
var Long;
function codeAddress()
{
var geocoder = new google.maps.Geocoder();
var addy1......
geocoder.geocode({ 'address': fullAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
Lat = results[0].geometry.location.lat();
Long = results[0].geometry.location.lng();
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
alert(Lat);
document.getElementById("Address_AddyLat").type.value = Lat;
document.getElementById("Address_AddyLong").value = Long;
}
感谢您的输入。
geocode
是一个异步函数,因此当您调用它时,它会立即返回,并在设置Lat
的值之前执行下一行。这样想:
geocoder.geocode({ 'address': fullAddress }, /*...*/); // 1
alert(Lat); // 2
document.getElementById("Address_AddyLat").type.value = Lat; // 3
document.getElementById("Address_AddyLong").value = Long; // 4
您要做的是实际读取回调本身中的Lat
值:
geocoder.geocode({ 'address': fullAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
Lat = results[0].geometry.location.lat();
Long = results[0].geometry.location.lng();
alert(Lat);
document.getElementById("Address_AddyLat").type.value = Lat;
document.getElementById("Address_AddyLong").value = Long;
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
我认为阿门有权这么做。您的元素引用必须不正确 试试这个:
document.getElementById("Address_AddyLat").value = Lat;
或
正如Ameen所说,地理代码是一个异步过程,所以您需要将警报和显示代码放入回调函数中。您的另一个错误是,您使用lat()&lng()作为一个方法,它不是一个方法,而是一个您只需要直接使用它的属性。所以你的代码看起来像
geocoder.geocode({ 'address': fullAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
Lat = results[0].geometry.location.lat;
Long = results[0].geometry.location.lng;
alert(Lat);
document.getElementById("Address_AddyLat").value = Lat;
document.getElementById("Address_AddyLong").value = Long;
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
看起来像是一个dup,它们并没有失去作用域,当您访问它们时,它们并没有被赋值
geocode
是一个异步函数,您需要将所有与其结果相关的内容放入callbackWell中。我最初尝试过这个方法……然后切换到这个方法。我可以再试一次。如果(status==google.maps.GeocoderStatus.OK){document.getElementById(“Address_addyla”).value=results[0]。geometry.location.lat();document.getElementById(“Address_AddyLong”).value=results[0]。geometry.location.lng();}没有更新表格吗???不管谁投了反对票,都可以用正确的答案摩擦我的鼻子。阿门,它不起作用。警报(Lat)具有适当的值,但文档元素不更新??那么,地址的类型是什么?它是类型为文本的输入
?如果是这样,则需要执行document.getElementById(“Address_addyla”).value=Lat
(不带类型)
geocoder.geocode({ 'address': fullAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
Lat = results[0].geometry.location.lat;
Long = results[0].geometry.location.lng;
alert(Lat);
document.getElementById("Address_AddyLat").value = Lat;
document.getElementById("Address_AddyLong").value = Long;
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});