【发布时间】:2011-10-02 00:13:28
【问题描述】:
谁能告诉我为什么这个提醒是空的?
var pending_dates = [];
$.getJSON('/ajax/event-json-output.php', function(data) {
$.each(data, function(key, val) {
pending_dates.push({'event_date' : val.event_date});
});
});
alert(pending_dates);
我无法理解这一点。我不是将pending_dates 声明为全局变量,可以在每个循环中访问吗?如何解决这个问题?
请注意,JSON 输出运行良好。如果我在getJSON function 中声明待定日期(并在该函数中声明),它可以工作,但我需要将数据存储在该getJSON 函数之外的数组中。
感谢您的贡献。
编辑
感谢您的 cmets,此代码可以正常工作:
pending_dates = [];
$.getJSON('/ajax/event-json-output.php', function(data) {
$.each(data, function(key, val) {
pending_dates.push({'event_date' : val.event_date});
});
}).success(function() { alert(pending_dates); })
非常感谢您的贡献!
【问题讨论】:
标签: javascript jquery variables scope each