var list = [];
$.getJSON("json.js", function(data) {
$.each(data, function(i, item) {
console.log(item.text);
list.push(item.text);
});
});
console.log(list.length);
list.length
always returns 0. I've browsed the JSON in firebug and it's well formed and everything looks fine. I just can't seem to add an item to the array what am I missing?
列表。长度总是返回0。我已经在firebug中浏览了JSON,它的格式很好,一切看起来都很好。我似乎不能向数组中添加项我缺少什么?
3 个解决方案
#1
113
Since $.getJSON
is async, I think your console.log(list.length);
code is firing before your array has been populated. To correct this put your console.log
statement inside your callback:
因为美元。getJSON是异步的,我想你的console.log(list.length);代码在填充数组之前启动。要纠正这个问题,请放置控制台。回调中的日志语句:
var list = new Array();
$.getJSON("json.js", function(data) {
$.each(data, function(i, item) {
console.log(item.text);
list.push(item.text);
});
console.log(list.length);
});
#2
3
You are making an ajax request which is asynchronous therefore your console log of the list length occurs before the ajax request has completed.
您正在发出一个异步的ajax请求,因此在ajax请求完成之前,您的控制台日志将显示列表长度。
The only way of achieving what you want is changing the ajax call to be synchronous. You can do this by using the .ajax and passing in asynch : false however this is not recommended as it locks the UI up until the call has returned, if it fails to return the user has to crash out of the browser.
实现所需的惟一方法是将ajax调用更改为同步。您可以通过使用.ajax并传入asynch: false来实现这一点,但是不建议这样做,因为它会锁住UI,直到调用返回,如果返回失败,用户就必须退出浏览器。
#3
3
Hope this will help you..
希望这对你有帮助。
var list = [];
$(document).ready(function () {
$('#test').click(function () {
var oRows = $('#MainContent_Table1 tr').length;
$('#MainContent_Table1 tr').each(function (index) {
list.push(this.cells[0].innerHTML);
});
});
});
#1
113
Since $.getJSON
is async, I think your console.log(list.length);
code is firing before your array has been populated. To correct this put your console.log
statement inside your callback:
因为美元。getJSON是异步的,我想你的console.log(list.length);代码在填充数组之前启动。要纠正这个问题,请放置控制台。回调中的日志语句:
var list = new Array();
$.getJSON("json.js", function(data) {
$.each(data, function(i, item) {
console.log(item.text);
list.push(item.text);
});
console.log(list.length);
});
#2
3
You are making an ajax request which is asynchronous therefore your console log of the list length occurs before the ajax request has completed.
您正在发出一个异步的ajax请求,因此在ajax请求完成之前,您的控制台日志将显示列表长度。
The only way of achieving what you want is changing the ajax call to be synchronous. You can do this by using the .ajax and passing in asynch : false however this is not recommended as it locks the UI up until the call has returned, if it fails to return the user has to crash out of the browser.
实现所需的惟一方法是将ajax调用更改为同步。您可以通过使用.ajax并传入asynch: false来实现这一点,但是不建议这样做,因为它会锁住UI,直到调用返回,如果返回失败,用户就必须退出浏览器。
#3
3
Hope this will help you..
希望这对你有帮助。
var list = [];
$(document).ready(function () {
$('#test').click(function () {
var oRows = $('#MainContent_Table1 tr').length;
$('#MainContent_Table1 tr').each(function (index) {
list.push(this.cells[0].innerHTML);
});
});
});