如何将jQuery变量传递给一个全球性 [英] How to pass JQuery variable to a global one

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

问题描述

我尝试从jQuery函数数据,并将其传递到全局变量与谷歌地图使用。这些变量必须保持全局的,否则谷歌地图不与他们合作。我设法让我从一个AJAX网址所需要的所有数据,并将其内只jQuery函数完美,但记录。如果我记录它在它之外,它的定义。反正是有这些值传递给全局变量?

 函数displayMarkers(){
    VAR经纬度=新google.maps.LatLng(latitd,longtd);
    变量名称= TITLENAME;
    createMarker(经纬度,名);
}VAR latitd;
VAR longtd;
VAR TITLENAME;$(文件)。就绪(函数(){
    $('#地震')。点击(函数(){
        getQuakes();
    });    功能getQuakes(){
        $阿贾克斯({
            网址:'http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime='+ yesterDate +'和;结束时间='+ displayDate,
            成功:功能(数据){
                的console.log(数据);
                $。每个(data.features,功能(键,VAL){
                    VAR坐标= val.geometry.coordinates;
                    locationD = {
                        LATD:坐标[0]
                        lngd:坐标[1]
                    };
                    latitd = locationD.latd;
                    longtd = locationD.lngd;
                    TITLENAME = val.properties.title;                    的console.log(latitd,longtd);
                    的console.log(TITLENAME);
                });
            }
        });
    }
});


解决方案

您code应该是这样的。

  VAR latitd;
VAR longtd;
VAR TITLENAME;$(文件)。就绪(函数(){
    $('#地震')。点击(函数(){
        getQuakes();
    });
});功能getQuakes(){
    $阿贾克斯({
        网址:'http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime='+ yesterDate +'和;结束时间='+ displayDate,
        成功:功能(数据){
            的console.log(数据);
            $。每个(data.features,功能(键,VAL){
                VAR坐标= val.geometry.coordinates;
                locationD = {
                    LATD:坐标[0]
                    lngd:坐标[1]
                };
                latitd = locationD.latd;
                longtd = locationD.lngd;
                TITLENAME = val.properties.title;                的console.log(latitd,longtd);
                的console.log(TITLENAME);                //调用此函数成功后,阿贾克斯在这里显示
                displayMarkers();
            });
        }
    });
}功能displayMarkers(){
    VAR经纬度=新google.maps.LatLng(latitd,longtd);
    变量名称= TITLENAME;
    createMarker(经纬度,名);
}

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);    
                });
            }    
        });    
    }    
});

解决方案

Your code should be like this

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天全站免登陆