如何将 JQuery 变量传递给全局变量 [英] How to pass JQuery variable to a global one

查看:45
本文介绍了如何将 JQuery 变量传递给全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 JQuery 函数中检索数据并将其传递到全局变量中以与 Google 地图一起使用.这些变量必须保持全局,否则 Google 地图无法使用它们.我设法从 AJAX url 获取我需要的所有数据,它可以完美记录,但仅在 Jquery 函数内部.如果我将它记录在它之外,它是未定义的.有没有办法将这些值传递给全局变量?

I'm trying to retrieve data from JQuery function and pass it into global variables to use with Google Maps. These variables have to stay global, otherwise Google Maps don't work with them. I manage to get all data that I need from AJAX url and it logs perfectly but only inside Jquery function. If I log it outside of it, it's undefined. Is there anyway to pass those values to global variables?

function displayMarkers() {    
    var latlng = new google.maps.LatLng(latitd, longtd);
    var name = titleName;
    createMarker(latlng, name);    
}  

var latitd;
var longtd;
var titleName;       

$(document).ready(function() {
    $('#earthquakes').click(function() {    
        getQuakes();
    });

    function getQuakes() {
        $.ajax({
            url: 'http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=' + yesterDate + '&endtime=' + displayDate,
            success: function(data) {
                console.log(data);
                $.each(data.features, function(key, val) {
                    var coord = val.geometry.coordinates;
                    locationD = {
                        latd: coord[0],
                        lngd: coord[1]
                    };
                    latitd = locationD.latd;
                    longtd = locationD.lngd;
                    titleName = val.properties.title;

                    console.log(latitd, longtd);
                    console.log(titleName);    
                });
            }    
        });    
    }    
});

推荐答案

你的代码应该是这样的

var latitd;
var longtd;
var titleName;

$(document).ready(function () {
    $('#earthquakes').click(function () {
        getQuakes();
    });
});

function getQuakes() {
    $.ajax({
        url: 'http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=' + yesterDate + '&endtime=' + displayDate,
        success: function (data) {
            console.log(data);
            $.each(data.features, function (key, val) {
                var coord = val.geometry.coordinates;
                locationD = {
                    latd: coord[0],
                    lngd: coord[1]
                };
                latitd = locationD.latd;
                longtd = locationD.lngd;
                titleName = val.properties.title;

                console.log(latitd, longtd);
                console.log(titleName);

                //Call this function to display here after success ajax
                displayMarkers();
            });
        }
    });
}

function displayMarkers() {
    var latlng = new google.maps.LatLng(latitd, longtd);
    var name = titleName;
    createMarker(latlng, name);
}

这篇关于如何将 JQuery 变量传递给全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆