【发布时间】:2010-01-30 10:33:29
【问题描述】:
我正在尝试让 jQuery.getJSON() 调用使用它返回的 JSON 数组更改全局变量
var photo_info ;
//Advance to the next image
function changeImage(direction) {
jQuery('img#preview_image').fadeOut('fast');
jQuery('#photo_main').css('width','740px');
if (direction == 'next') {
jQuery.getJSON('/ajaxupdate?view&sort='+sort+'&a='+a+'&s=' + title_url_next, function(data) {
photo_info = data;
title_url = photo_info.title_url;
title_url_next = photo_info.preview_title_url_next;
title_url_previous = photo_info.preview_title_url_previous;
});
} else if (direction == 'prev') {
jQuery.getJSON('/ajaxupdate?view&sort='+sort+'&a='+a+'&s=' + title_url_previous, function(data) {
photo_info = data;
title_url = photo_info.title_url;
title_url_next = photo_info.preview_title_url_next;
title_url_previous = photo_info.preview_title_url_previous;
});
}
}
但是,变量 photo_info 只能从 getJSON() 函数中访问,并从脚本中的其他任何地方返回 undefined。
我做错了什么?感谢您的帮助。
【问题讨论】:
标签: javascript javascript-events jquery