2010-08-10 26 views
0

我有一個從$ .ajax函數返回的列表。我想將返回的列表添加到表中。以下是我正在使用的代碼片段。如何在表jQuery中添加一個列表?

$(document).ready(function() { 
$.ajax({ 
    type: "POST", 
    url: "Home/LoadTable", 
    success: function(data) { 
     var loopList = data.message.NewList; 
     alert(loopList); 
     //tried the following :(
     //loopList.each(function(i) { 
     // addRecentData(i); 
     //}); 
    }, 
    error: function() { 
     alert("ERROR"); 
    } 
}); 
}); 

function addRecentData(data) { 
$('#newTable tr:last').after('<tr><td class="date"></td><td class="name"></td></tr>'); 

var $tr = $('#newTable tr:last'); 
$tr.find('.date').html(data.message); 
$tr.find('.name').html(data.message.NewList[0].Name.toString()); 
} 

<table id = "newTable">        
    <tr> 
     <td class="date"></td> 
     <td class="polNum"></td> 
    </tr> 
</table> 
+0

你的JSON回來的樣子是什麼? – 2010-08-10 19:53:18

+0

我有JSON返回列表(NewList)。警報消息確實顯示對象存在,用逗號分隔列表對象([Object object],[Object object] ...)。 $ tr.find( '名')的HTML(data.message.NewList [0] .Name.toString())。還在列表中顯示第一個名字 – MrM 2010-08-10 20:32:00

+0

@ user54197 - 那麼約會呢?目前尚不清楚日期屬性位於json對象上 – 2010-08-10 20:48:40

回答

1

像這樣的東西應該爲你工作: -

for (i = 0; i <= data.message.NewList.length - 1; i++) { 
     $('#newTable > tbody:last').after('<tr><td class="date">' + data.message.NewList[i].Date + '</td><td class="name">' + data.message.NewList[i].Name.toString() + '</td></tr>'); 
    }; 

有關使用的詳細信息,請參閱Add table row in jQuery '> TBODY:最後的' jQuery選擇

相關問題