JSFiddle在这里:http: //jsfiddle.net/xgTt2/3/
我在 a 中有一个$.each
嵌套$.each
,我不确定为什么第二个$.each
运行两次。有任何想法吗?
var serverResponse = [{"Id":"aaa","OrderItems":[{"Id":1,"Description":"Salad"},{"Id":2,"Description":"Pizza"}]},{"Id":"bbb","OrderItems":[{"Id":3,"Description":"Salad"},{"Id":4,"Description":"Pizza"}]}];
$.each(serverResponse, function (index) {
var pos = serverResponse[index];
$('#placeholder').append('<p>' + pos.Id + '</p>')
$.each(pos.OrderItems, function (index) {
$('.orderitem').append('<p>' + this.Id +
' ' + this.Description + '</p>')
});
});
上面的 javascript 产生以下输出:
aaa
1 Salad
2 Pizza
3 Salad
4 Pizza
bbb
3 Salad
4 Pizza
我要这个:
aaa
1 Salad
2 Pizza
bbb
3 Salad
4 Pizza
知道我做错了什么吗?这是问题的一个工作示例:http: //jsfiddle.net/xgTt2/3/