我想添加新的數據(從JSON)到現有的表(使用jQuery)。從JSON添加新的數據到現有的表
在我的HTML我有這個表,例如:
<table data-role="table" data-mode="columntoggle" class="ui-responsive" data-column-btn-text="Filter Columns" id="MyTable">
<thead>
<tr>
<th data-priority="1">A</th>
<th data-priority="2">B</th>
<th data-priority="3">C</th>
</tr>
</thead>
<tbody>
<tr>
<td>a1</td>
<td>b1</td>
<td>c1</td>
</tr>
</tbody>
</table>
我試圖做到這一點(從JSON添加新的數據):
var response = [{
"A":"a2",
"B":"b2",
"C":"c2"
},
{
"A":"a3",
"B":"b3",
"C":"c3"
},
{
"A":"a4",
"B":"b4",
"C":"c4"
}];
$.each(response, function(i, item) {
$('<tr>').html(
//"<tr>" +
"<td>" + response[i].A + "</td><td>" + response[i].B + "</td><td>" + response[i].C + "</td>" + "</tr>").appendTo('#MyTable');
});
爲什麼它不工作?
你是否同意,如果追加到'#MyTable',新的元素將超出'tbody'或'thead'元素? – DontVoteMeDown
是的,我同意你的意見。我不知道如何改變/修復它。 – yoka
我希望我能幫上忙。謝謝。 – DontVoteMeDown