如何使用javascript AJAX响应动态创建表

如何使用javascript AJAX响应动态创建表,javascript,html,ajax,Javascript,Html,Ajax,我正在研究javascript。 我的代码中有一些使用Ajax的API调用。 我的UI中有一个按钮 单击此按钮后,我将使用AJAX进行一些API调用,并显示以下HTML UI: 上表中有两行。现在,如果我关闭这个弹出窗口,然后再次单击用户仪表板按钮,它将在表中再次追加这两行。我不想再附加那些行 我使用AJAX响应生成表的代码如下所示: getUserAccountDetailsCallback : function(userid, appid, response){ if

我正在研究javascript。 我的代码中有一些使用Ajax的API调用。 我的UI中有一个按钮

单击此按钮后,我将使用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);
                  }

              }

          }
      }
请帮忙。我做错了什么

添加ad和tbody 网关。。。。。 在添加之前,请先删除tbody中的内容 像这样:

var tBody = document.getElementById("tBodyID");
while (tBody.firstChild) {
  tBody.removeChild(tBody.firstChild);
}
if(res.subscriptionsForCurrentAppId.length > 0){

您可以在关闭后或追加新文件之前从表中删除tr。或者在exist tr中重写除append new之外的数据。@Sojtin您能在代码级别向我提出建议吗?