【发布时间】:2023-03-29 05:02:02
【问题描述】:
对于所有 javascript/jquery 人来说,这将是一个非常简单的问题。
过去 2 年我一直在编写 javascript,今天我遇到了一个奇怪的问题。
我正在从我的C# Webmethod 获取一个 JSONarray,并由jQuery.ajax() 调用它
当我这样做时
for (i in JSONRaw) {
Index = Index + 1;
$('#bottomGridDashboard').append('<tr> <td>' + Index + '</td> <td>' + JSONRaw[i].DisplayName + '</td> <td>' + JSONRaw[i].SpeedInKPH + '</td> <td><img src="' + JSONRaw[i].ImageURL + '" height="20px" alt="" /></td> <td>' + JSONRaw[i].DepotID + '</td> <td>' + JSONRaw[i].RouteID + '/' + JSONRaw[i].ScheduleID + '/' + JSONRaw[i].TripID + '</td> <td>' + JSONRaw[i].Direction + '</td> <td>' + JSONRaw[i].LastStop + '</td> <td> ' + JSONRaw[i].ETAMinutes + ' </td> </tr>');
}
附加表添加了两个额外的行,将每个字段都设置为“未定义”。 See this Image
但是,如果我用
替换循环for (var i = 0; i < JSONRaw.length;i++ ) {
Index = Index + 1;
$('#bottomGridDashboard').append('<tr> <td>' + Index + '</td> <td>' + JSONRaw[i].DisplayName + '</td> <td>' + JSONRaw[i].SpeedInKPH + '</td> <td><img src="' + JSONRaw[i].ImageURL + '" height="20px" alt="" /></td> <td>' + JSONRaw[i].DepotID + '</td> <td>' + JSONRaw[i].RouteID + '/' + JSONRaw[i].ScheduleID + '/' + JSONRaw[i].TripID + '</td> <td>' + JSONRaw[i].Direction + '</td> <td>' + JSONRaw[i].LastStop + '</td> <td> ' + JSONRaw[i].ETAMinutes + ' </td> </tr>');
}
未定义的行消失了..See the image
我为我的愚蠢道歉,但我以前从未遇到过这样的问题。 可能是什么原因??
编辑:
数组非常好。而且里面没有洞。 另一个观察结果是未定义的属性只出现在我的网格底部。我认为它处理的索引比数组长度多两个。
EDIT-2
我的 console.log 显示了我的数组中的以下元素。
http://i.imgur.com/rI5TjdK.jpg
我已经在我的母版页中声明了原型。
Array.prototype.inArray = function (comparer) {
for (var i = 0; i < this.length; i++) {
if (comparer(this[i])) return true;
}
return false;
};
Array.prototype.pushIfNotExist = function (element, comparer) {
if (!this.inArray(comparer)) {
this.unshift(element);
}
};
它是否增加了数组长度。 ? ?
【问题讨论】:
-
把你问题的全部内容放在你的问题中,不要链接到站外。您可以发布图片,尽管使用
console.log输出,最好将其包含为文本。 -
我以后会处理的.. :)
标签: javascript jquery for-loop