我在jQuery中工作,我有一个变量,我声明为全局函数,当我提醒它时,它给了我正确的结果,但现在我想访问相同的在另一个函数中变量,并在另一个函数中提醒它它给我空结果意味着我不能在那里访问它。我知道一个变量的范围,但为了克服它,我把变量解释为全局的,但我仍然无法访问它。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被上面的函数decleared,我想只是旁边访问与其他函数值相同的变量在同一个脚本标签。
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');
}
});
}
请告诉我它有什么问题。预先感谢任何帮助。
well ajax is ASYNC ..在sendEmail函数中获取变量的唯一方法是在第一个函数的ajax完成中调用它。和是**你不需要准备功能内功能回调** – bipen
对不起,我不明白我没有得到的点 –