jquery内部$。每个(函数)都希望创建变量,然后在函数外部使用变量
下面是一个例子 函数内部jquery内部$。每个(函数)都希望创建变量,然后在函数外部使用变量,jquery,Jquery,下面是一个例子 函数内部$。每个(地址、函数(索引、项){I定义变量纬度和经度 然后,在函数完成后,要提醒(保留)这些值 预期警报(latitude+“last latitude Expect alert”);在警报之后发出警报(latitude+“latitude Expect alert first”);,但它首先发出警报,值为null 需要纠正什么 这是密码 var latitude = null; var longitude = null; $(function() { var ad
$。每个(地址、函数(索引、项){
I定义变量纬度
和经度
然后,在函数完成后,要提醒(保留)这些值
预期警报(latitude+“last latitude Expect alert”);
在警报之后发出警报(latitude+“latitude Expect alert first”);
,但它首先发出警报,值为null
需要纠正什么
这是密码
var latitude = null;
var longitude = null;
$(function() {
var address = [ "Collins+Street,+MELBOURNE,+VIC,+AUSTRALIA,+3000", "Sussex+Street,+SYDNEY,+NSW,+AUSTRALIA,+2000", "George+Street,+BRISBANE,+QLD,+AUSTRALIA,+4000"];
$.each(address, function(index, item) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': item}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
}
alert( latitude + ' latitude expect alert first' );
});//geocoder.geocode
});//$.each(address, function(index, item) {
alert( latitude + ' latitude expect alert last' );
});
alert( latitude + ' latitude expect alert last outside function' );
ajax的臭味…只是一个旁注建议,使用
console.log()
而不是alert()
。更方便、更美观。让我们来看看。对geocoder的调用是异步的。在数据返回之前,您无法访问数据。在回调中执行您需要执行的任何操作。不确定首先要执行什么操作,