簡體   English   中英

如何獲得成功的Ajax響應后動態創建表?

[英]How do I create a table dynamically upon getting a successful ajax response?

我正在從數據庫中返回數據並動態創建表。 如果對page1.php的調用成功,我們將創建一個包含一個標題行的表,然后遍歷結果並添加一些行。 如何添加單個標題行? 謝謝!

$.post("page1.php", {user: "homer"}, 
function(data){
// output the header row
  function(test){
  var tblHdr ='<th>'+'Username'+'</th>';
  $(tblHdr).appendTo("#tble");
  };

// output the user data
   $.each(data.userdata, 
     function(i,details){

     var tblRow ='<tr>'+'<td>'details.name+'</td>'+'</tr>';
         $(tblRow).appendTo("#tble");

    });
   }, "json"
);

您為什么甚至需要或想要那里的功能? 如果您從該塊中刪除整個函數聲明,然后僅按定義添加th,那么您應該可以。 假設在某處定義了tblHdr和tblRow。

$.post("page1.php", {user: "homer"}, 
function(data){
// output the header row

  var tblHdr ='<th>'+'Username'+'</th>';
  $(tblHdr).appendTo("#tble");


// output the user data
   $.each(data.userdata, 
     function(i,details){

     var tblRow ='<tr>'+'<td>'details.name+'</td>'+'</tr>';
         $(tblRow).appendTo("#tble");

    });
   }, "json"
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM