以下代码获取一个 JSON 对象,然后将其内容吐出到 <table>
中。 。我第一次这样做时,我得到了 JSON 内容。但是,当我刷新时,刷新的数据会粘在表格的底部。如何刷新数据以仅显示新数据?我尝试使用 .remove()
但有明显的删除,然后刷新数据。
$(function() {
$('#ReportedIssue').change(function() {
//$('.data').remove()
$.getJSON('/CurReport/GetUpdatedTableResults', function(json) {
for (var i = 0; i < json.GetDocumentResults.length; i++) {
$('#DocumentInfoTable').append(
"<tr class='data'>" +
"<td>" + json.GetDocumentResults[i].Document.DocumentId + "</td>" +
"<td>" + json.GetDocumentResults[i].Document.LanguageCode + "</td>" +
"<td>" + json.GetDocumentResults[i].ReportedIssue + "</td>" +
"<td>" + json.GetDocumentResults[i].PageNumber + "</td>" +
"</tr>"
);
};
});
});
});
谢谢,
亚伦
最佳答案
按如下方式构建 HTML 会更有效(当然,也可以解决您遇到的问题):
$(function() {
$('#ReportedIssue').change(function() {
//$('.data').remove()
$.getJSON('/CurReport/GetUpdatedTableResults', function(json) {
var str = '';
for (var i = 0; i < json.GetDocumentResults.length; i++) {
str += "<tr class='data'>" +
"<td>" + json.GetDocumentResults[i].Document.DocumentId + "</td>" +
"<td>" + json.GetDocumentResults[i].Document.LanguageCode + "</td>" +
"<td>" + json.GetDocumentResults[i].ReportedIssue + "</td>" +
"<td>" + json.GetDocumentResults[i].PageNumber + "</td>" +
"</tr>"
};
$('#DocumentInfoTable').html(str);
});
});
});
关于javascript - 使用 jQuery .append() 后刷新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2541278/