【发布时间】:2021-04-23 03:05:20
【问题描述】:
我正在尝试从服务器获取数据,这是一长串对象,例如:
{
"id": ,
"product": "Candy",
"Country": "",
"date": "",
"amount": ,
"image": "candy.jpg"
"id": ,
"product": "Chips",
"Country": "",
"date": "",
"amount": ,
"image": "chips"
}
正如您在上面看到的,这是一行接一行,我在 Javascript 中有一个函数,我从服务器获取数据并将其放入表中。但它只给了我所有的数据,而不是在单独的行中。
Candy Africa June 2022 75 kg candy.jpg Chips America July 2022 65 kg chips.jpg
所以最后我想要一张这样的表格:
(想象成一张桌子)
Product Country Date Amount Image
Candy Africa June 2022 75 kg candy.jpg
Chips America July 2022 65 kg chips.jpg
这是我现在的代码:
function createTable() {
$(document).ready(function () {
setTimeout(function () {
$.ajax({
url: "LINK",
type: 'GET',
dataType: 'json',
success: function (data) {
$("#inputTable").html(data);
$(data).each(function (index, info) {
$('#inputTable').append('<td>' + info.product + '</td>') +
$('#inputTable').append('<td>' + info.country + '</td>') +
$('#inputTable').append('<td>' + info.date + '</td>') +
$('#inputTable').append('<td>' + info.amount + '</td>' ) +
$('#inputTable').append('<td>' + info.image + '</td>')
});
}
});
}, 1);
});
}
【问题讨论】:
-
为什么要将 jQuery 方法调用与
+连接起来?
标签: javascript html dynamic html-table