以下是将'undefined'作为每个项目的值返回。有人能告诉我为什么吗?
Jquery的
$("button").click(function () {
var estimateID = this.id;
var baseURL = "/Dashboard/EstimateDetails/";
var url = baseURL + estimateID;
$.getJSON(url, function (data) {
for (var i = 0; i <= data.details.length; i++) {
$('#Details').html("<p>item1=" + data.details.Dma + " item2=" + data.details.Callsign + " item3=" + data.details.Description + "</p>");
}
});
JSON
{
"details": [{
"Dma": "Albany-Schenectady-Troy",
"CallSign": "WRGB",
"Description": "WRGB (CBS) Schenectady"
}, {
"Dma": "Albany-Schenectady-Troy",
"CallSign": "WTEN",
"Description": "WTEN (ABC) Albany "
}, {
"Dma": "Albany-Schenectady-Troy",
"CallSign": "WXXA",
"Description": "WXXA (Fox) Albany "
}, {
"Dma": "Atlanta",
"CallSign": "WGCL",
"Description": "WGCL (CBS) Atlanta "
}, {
"Dma": "Atlanta",
"CallSign": "WXIA",
"Description": "WXIA (NBC) Atlanta "
}, {
"Dma": "Austin",
"CallSign": "KXAN",
"Description": "KXAN (NBC) Austin "
}, {
"Dma": "Austin",
"CallSign": "KVUE",
"Description": "KVUE (ABC) Austin "
}, {
"Dma": "Baltimore",
"CallSign": "WMAR",
"Description": "WMAR (ABC) Baltimore "
}, {
"Dma": "Baltimore",
"CallSign": "WBAL",
"Description": "WBAL (NBC) Baltimore"
}, {
"Dma": "Baltimore",
"CallSign": "WJZ ",
"Description": "WJZ (CBS) Baltimore "
}, {
"Dma": "Baltimore",
"CallSign": "WBFF",
"Description": "WBFF (Fox) Baltimore "
}]
}
编辑 - 运行上述代码时的结果是添加到详细信息div的单行HTML,如下所示:
item1=undefined item2=undefined item3=undefined
答案 0 :(得分:3)
data.details.Callsign
错了
data.details[i].Callsign
是你想要的,也就是你循环details
时缺少数组索引。请注意,您正在为多个变量执行此操作。
清洁代码可能有所帮助。像这样分离变量是一个好的开始......
$.getJSON(url, function (data) {
var details = data.details,
dma, callSign,...; // and the rest of whatever you need
for (var i = 0; i <= details.length; i++) {
dma = details[i].Dma;
callSign = details[i].Callsign;
// more here
$('#Details').html("<p>item1=" + dma + " item2=" + callSign + ...
}
});
更具可读性,更不容易出错恕我直言。
编辑 - 正如评论中所指出的,html
每次都会在循环中覆盖div的内容。您可能需要append
。
答案 1 :(得分:1)
你在循环中省略了数组索引。
$("button").click(function () {
var estimateID = this.id;
var baseURL = "/Dashboard/EstimateDetails/";
var url = baseURL + estimateID;
$.getJSON(url, function (data) {
for (var i = 0; i <= data.details.length; i++) {
$('#Details').html("<p>item1=" + data.details[i].Dma + " item2=" + data.details[i].Callsign + " item3=" + data.details[i].Description + "</p>");
}
});
答案 2 :(得分:1)
您已将details
定义为数组,因此需要对其进行索引以访问其成员。例如,在for循环中,您需要这种类型的东西:
data.details[i].Dma
而不是
data.details.Dma
顺便说一句,你可以使用jQuery每个回调而不是for循环:
if (data && data.details) {
$.each(data.details, function() {
$('#Details').html("<p>item1=" + this.Dma + " item2=" + this.Callsign +
" item3=" + this.Description + "</p>");
});
}
在传递给每个回调函数的回调函数中,this
代表for循环中的data.details[i]
。
答案 3 :(得分:1)
使用
data.details[i].Dma
等
同样在循环-1
中添加data.details.length-1
,因为索引以0
开头。
for (var i = 0; i <= data.details.length-1; i++) {
$('#Details').html("<p>item1=" + data.details[i].Dma + " item2=" + data.details[i].Callsign + " item3=" + data.details[i].Description + "</p>");
}
答案 4 :(得分:0)
在for循环中,您需要通过data.details[i]
例如
data.details[i].Callsign
不
data.details.Callsign
答案 5 :(得分:0)
将索引添加到data.details的末尾
data.details.Dma
变为data.details[i].Dma