【发布时间】:2018-09-11 13:19:44
【问题描述】:
我使用 AJAX 并获得 JSON 响应,然后我想将 json 映射到表 html 喜欢
# |类型ID |类型描述 |创建者
1 | 000001 | AAAAAA |亚当
2 | 000002 | BBBBBB |詹姆斯
这是我的json
{
"records": [{
"type_id": 000001,
"type_desc": "AAAAAA ",
"type_createby": "Adam"
}, {
"type_id": 000002,
"type_desc": "BBBBBB",
"type_createby": "James"
}, {
"type_id": 000003,
"type_desc": "CCCCCC",
"type_createby": "Sam"
}]
}
这是我正在尝试的
success: function (response) {
$('#table-container').html("");
$.each(response, function (index) {
var tableRow = "";
var row = 1;
tableRow = $('<tr/>');
tableRow.append("<td>" + row + "</td>");
row = row + 1;
tableRow.append("<td>" + response[index].type_id + "</td>");
tableRow.append("<td>" + response[index].type_desc + "</td>");
tableRow.append("<td>" + response[index].type_createby + "</td>");
$('#table-container').append(tableRow);
});
},
我的显示器显示了表格,但数据仍然显示“未定义”。我有两个问题。
1.如何定义正确的数据?
2.如何循环显示5行并使用javascript进行分页?
【问题讨论】:
-
确保
response是一个对象,而不是 JSON 字符串,然后遍历response.records而不是response。然后只需检查index是否大于四,在这种情况下从each的回调中返回false。请注意,如果您希望它继续计数,则需要在each的回调之外声明row。
标签: javascript jquery html json ajax