【发布时间】:2021-07-07 15:57:12
【问题描述】:
将 json 数据显示到 html 表后出现问题。
问题;
- 每次更改product_id时,表值总是添加新数据。
- 如果结果为空,tbody 表仍然显示当前之前的数据。
这是我的代码:
<table class="table" id="history_maintenance">
<thead>
<th>Date In</th>
<th>SPK No</th>
<th>Purchase No</th>
<th>Maintenance Notes</th>
</thead>
<tbody></tbody>
</table>
<script>
$('#product_id').on('change', function() {
let customer_name = document.getElementById("customer_name").value;
let phone = document.getElementById("phone").value;
let car_plate_no = document.getElementById("car_plate_no").value;
let car_type = document.getElementById("car_type").value;
let product_id = document.getElementById("product_id").value;
$.ajax({
type: "POST",
url: "<?php echo base_url('spk/get_customer_transaction_history') ?>",
dataType: "json",
cache: false,
data: {
customer_name: customer_name,
phone: phone,
car_plate_no: car_plate_no,
car_type: car_type,
product_id: product_id,
},
success: function(dataHistory) {
let transactionHistory = '';
for (i = 0; i < dataHistory.length; i++) {
transactionHistory += '<tr>';
transactionHistory += '<td>' + dataHistory[i].date_in + '</td>';
transactionHistory += '<td>' + dataHistory[i].spk_no + '</td>';
transactionHistory += '<td>' + dataHistory[i].purchase_no + '</td>';
transactionHistory += '<td>' + dataHistory[i].spk_maintenance_notes + '</td>';
transactionHistory += '</tr>';
}
$('#history_maintenance tbody').append(transactionHistory);
}
});
return false;
});
</script>
【问题讨论】:
-
这就是附加的意思——添加到已经存在的内容。看来您只需要覆盖文本内容。
-
如何覆盖文本内容?我的意思是我可以学习什么功能或文章来解决这个问题?
-
这只是一个简单的分配:
yourElement.textContent = yourValue。 -
或者,如果你想坚持使用 jQuery 函数,那就是
.html()。
标签: javascript php html json