【发布时间】:2011-06-15 09:04:48
【问题描述】:
这可能是非常愚蠢的事情,但对于我的生活我看不到它。
我有用字符串填充数组的代码,然后我想循环遍历数组并将值作为选项添加到选择控件中。但是由于某种原因,当我调用长度属性时它返回 0,但是如果我使用 console.dir 在控制台中显示数组,它里面有数据。
代码如下:
var sectors = new Array();
// code to add values
$.getJSON("get/tech_info.php", {uid:json.uid}, function(data){
$.each(data, function(i, json){
if($.inArray(json.area, sectors) == -1)
{
sectors[sectors.length] = json.area;
}
});
});
console.dir(sectors);
var sec = "#mainSectors" + count;
console.log(sectors.length); // returning 0
for ( var i=0; i != sectors.length; i++ ){
console.log("bla bla bla");
$(sec).append(
$("<option></option>").html(sectors[i])
);
}
这是你可以看到的 cosole.dir 数据有数据:
//secotrs
Array[0]
0: "Industrial"
1: "Commercial"
length: 2
__proto__: Array[0]
【问题讨论】:
-
你能显示你实际使用
console.dir的代码吗?以及添加值的代码? -
我认为问题出在
// code to add values goes here //。 -
我看到你的数组是多维的
-
如果我去扇区[0].length它会抛出一个错误,这只是console.dir显示数据的方式
-
错误是什么?如果
sectors[0] = "Industrial",那么sectors[0].length应该显示10(字符串的长度)。
标签: javascript