如何在表jquery中添加列表?

时间:2010-08-10 19:39:38

标签: jquery ajax

我有一个从$ .ajax函数返回的列表。我想将返回的列表添加到表中。下面是我正在使用的代码片段。

$(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>

1 个答案:

答案 0 :(得分: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>');
    };

有关使用'&gt;的更多信息,请参阅Add table row in jQuery tbody:最后一个'jquery选择器