jQuery访问从一个函数到另一个函数的全局变量的值

时间:2013-10-28 11:30:38

标签: javascript jquery ajax

我在jQuery工作,我有一个变量,我在一个函数中声明为全局,当我提醒它时它给了我正确的结果但现在我想在另一个函数中访问相同的变量并在另一个函数中提醒它给我空的结果意味着我无法在那里访问它。我知道变量的范围,但为了克服它,我将变量视为全局变量,但我仍然无法访问它。

这是我的第一个功能:

 var employee_email = '';
 function showCustomer()
 {
 // fire off the request to ajax_stufflist.php
request = $.ajax({
    url: "ajax_stufflist.php?"+url,
    type: "post",
    success: function(data){
        if(data != ''){
    var response = $(data).find("#gmp_stuff").html();
    employee_email = $(data).find(".EMP_EMAIL>span").html();
    //alert(employee_email);
    $("#user_responses").html(response);
      $(function() {
            $("#user_responses").dialog({
                dialogClass:'transparent',
                resizable: false,
                draggable: false,
                modal: true,

                width: 1000,
                autoOpen: false,
                overlay: { opacity: 0 }
            });

  $('#user_responses').dialog('open');
  $('#user_responses').css('display','');


    });
        }
  },
    error:function(){
        alert("failure");
        $("#user_responses").html('error occured');
    }
  });

}

在此函数中,变量employee_email在函数上方被删除,我想在相同脚本标记中的旁边的其他函数中访问具有值的相同变量。

function sendEmail(){
alert(employee_email );
request = $.ajax({
    url: "send_email.php?"+employee_email ,
    type: "post",
    success: function(data){
    $("#email_responses").html();


    },
    error:function(){
        alert("failure");
        $("#email_responses").html('error occured');
    }
  });
 }

请告诉我它有什么问题。提前感谢您的任何帮助。

3 个答案:

答案 0 :(得分:1)

这不是范围问题。如果可能的话,你的控制台就会出错。但是你得到一个空的结果,因为AJAX调用尚未完成,或者由于某些错误而无法更改值。试试这个。
定义:

var flag = false;

您的成功功能应该是:

success: function (data) {
                if (data != '') {
                    var response = $(data).find("#gmp_stuff").html();
                    employee_email = $(data).find(".EMP_EMAIL>span").html();
                    //alert(employee_email);
                    $("#user_responses").html(response);
                    //$(function () {
                    $("#user_responses").dialog({
                        dialogClass: 'transparent',
                        resizable: false,
                        draggable: false,
                        modal: true,

                        width: 1000,
                        autoOpen: false,
                        overlay: { opacity: 0 }
                    });

                    $('#user_responses').dialog('open');
                    $('#user_responses').css('display', '');
                    //});
                    flag = true;
                }
            }

sendEmail()可以是:

function sendEmail(){
    if(flag)
    {
        request = $.ajax({
            url: "send_email.php?"+employee_email ,
            type: "post",
            success: function(data){
                $("#email_responses").html();
            },
            error:function(){
                alert("failure");
                $("#email_responses").html('error occured');
            }
        });
    }
    else
        alert('Sending email is unavailable currently. PLease try after some time.');
}

答案 1 :(得分:1)

尽管在SO上已经回答了数百次,但我可以给你一个简短的解释。

由于您正在处理2个异步调用,因此您无法在正常的同步流中访问第一个调用的数据。

让您的第一个功能返回$.ajax

function showCustomer(){
    return $.ajax({ /* ... */ });

并访问回调中返回的数据:

showCustomer().done(function(data){
    console.log($(data).find(".EMP_EMAIL>span").html());
});

这假设您正在使用jQuery 1.5或更高版本,其中ajax调用公开了一个promise。

另一种选择是嵌套ajax调用(在第一个成功处理程序中调用第二个)。

答案 2 :(得分:1)

您正在通过AJAX调用更新值employee_email。一旦第一个函数被调用,它就会调用AJAX并转移到第二个函数。第二个函数尚未看到值更改,employee_email根本不会更改。 你有两个选择 -

  • 在完成第一个功能时使用第二个功能。
  • 在第一次调用中使用Deferred对象,并在完成后调用第二个函数。