2
meclass.prototype.switch = function() {
var items = [];
$.getJSON('http://localhost/jsoner.php', function(data) {
$.each(data, function(key, val) {
items.push(val);
alert(items[0]); //this works
});
});
alert(items[0]); //this does not
}
나는 이것을 잠시 동안 만지작 거리며 실제로 얻지 못했습니다. 나는 모든 jquery 함수에서이 문제를 겪고있다. 그래서 나는 방금 배운 것이 없어서 대답을 찾지 못했다.함수 내에서 jquery 변수 범위
항목 배열이 당신이 그것을 액세스하려고 할 때 비어 있습니다. 서버에서 JSON 객체를받을 때만 채워집니다. 따라서 콜백에서 모든 것이 잘 작동합니다. – skovalyov