我正在研究 JavaScript。我的代码中有一些 API 调用(使用 AJAX)。我的用户界面中有一个按钮,上面写着USER DASHBOARD
。单击此按钮后,我将使用 AJAX 进行一些 API 调用,并显示包含包含行的表的 HTML UI。
上表中有两行。如果我关闭此弹出窗口,然后再次单击“用户仪表板”按钮,它将再次将这两行追加到表中。我不想再次追加这些行。
我使用 AJAX 响应形成表格的代码如下所示:
getUserAccountDetailsCallback: function (userid, appid, response) {
if (response != "") {
var res = JSON.parse(response);
var totalNoOfApps = document.getElementById('totalSites');
var totalNoOfSubscriptions = document.getElementById('totalSubscribers');
totalNoOfApps.innerHTML = res.totalNoOfApps;
totalNoOfSubscriptions.innerHTML = res.totalNoOfSubscriptions;
if (res.subscriptionsForCurrentAppId.length > 0) {
for (var i = 0; i < res.subscriptionsForCurrentAppId.length; i++) {
var td1 = document.createElement('td');
td1.style.width = '30';
td1.innerHTML = i + 1;
var td2 = document.createElement('td');
td2.innerHTML = res.subscriptionsForCurrentAppId[i].gatewayName;
var td3 = document.createElement('td');
td3.innerHTML = res.subscriptionsForCurrentAppId[i].priceCurrencyIso;
var td4 = document.createElement('td');
td4.innerHTML = res.subscriptionsForCurrentAppId[i].amountPaid;
var date = new Date(res.subscriptionsForCurrentAppId[i].subscribedDate);
date.toString();
var td5 = document.createElement('td');
td5.innerHTML = date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear(); //res.subscriptionsForCurrentAppId[i].subscribedDate;
var td6 = document.createElement('td');
td6.innerHTML = res.subscriptionsForCurrentAppId[i].transactionId;
var td7 = document.createElement('td');
td7.innerHTML = res.subscriptionsForCurrentAppId[i].active;
var tr = document.createElement('tr');
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
tr.appendChild(td5);
tr.appendChild(td6);
tr.appendChild(td7);
var table = document.getElementById('tbl');
table.appendChild(tr);
}
}
}
}
请帮忙。我哪里做错了。
最佳答案
要清理表格,您需要将其 HTML 内容设置为空。
类似这样的事情:
var table = document.getElementById('tbl');
table.innerHTML = "";
之后,添加新行。
关于javascript - 使用Javascript ajax响应动态创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30343484/