我正在尝试用一些行填充一个表,所有工作都很好,除了实际上只附加了数组中的最后一行。我真的不知道为什么。我已经打印了数组,一切都应该如此。
$table_body = $('#tbody');
$table_row = $('<tr><td style="text-align: center" class="nome"></td><td style="text-align: center" class="email"></td></td><td style="text-align: center" class="confirmado"></td></td><td style="text-align: center" class="dataCandidatura"></td><td class="btn" style="text-align: center"></td></tr>');
for (i in data)
{
var future_field = data[i];
console.log(future_field);
$table_row.find('.nome').html(future_field.nome);
$table_row.find('.email').html(future_field.email);
$table_row.find('.confirmado').html(future_field.confirmed);
$table_row.find('.dataCandidatura').html(future_field.created_at);
// Appending table row to tbody
$table_body.append($table_row);
}
如果有人有任何线索,我将不胜感激。
最佳答案
尝试一下
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>
<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}
</script>
</body>
</html>