2009-10-17 115 views
0
$(document).ready(function(){ 
var refreshId = setInterval(function() { 
    periodicRefresh(); 
}, 9000); 
}) 

動態添加一行到表格

function periodicRefresh() 
{ 
    $.ajax({ 
     type: 'GET', 
     url: 'include/ajaxActions.php', 
     data: "action=displayLastEvent", 

     success: function(msg){ 
      var newid = $(msg).attr("id"); 
        current = $("#list tr:first").get(0).id; 
        if(newid != currentfirstrow){ 
         $("#list").prepend(msg); 
       lastrow = current-19; 
       $('#'+lastrow).remove(); 
        } 
     } 
    }); 
} 

這裏是什麼樣的表格行看起來像一個例子:

<tr id="5094" style="background:White;"> 
    <td class="tdstd"> 
    </td> 
</tr> 

回答

3

這一點很難回答,不知道如何將XML這是從「ajaxActions.php」返回的,並且您的AJAX調用已格式化。另外,我假設「currentfirstrow」是一個可在任何函數內部訪問的全局JavaScript變量?

function periodicRefresh() 
{ 
    $.ajax({ 
     type: 'GET', 
     url: 'include/ajaxActions.php', 
     data: "action=displayLastEvent", 

     success: function(xml){ 
      var newfirstrow = $(xml).find("name_of_xml_node_that_contains_newly_returned_id").text(); 
      if(newfirstrow != currentfirstrow){ 
       //do add new row() 
      } 
     }, 
} 
+0

ajaxActions.php的輸出僅是: Roch 2009-10-17 20:57:04

+0

但我也可以將它格式化爲XML – Roch 2009-10-17 20:58:25

+0

好的,所以你只是在輸出html。很高興知道,讓我考慮一下。 – WesleyJohnson 2009-10-17 21:20:56

1

起初IDS不允許是數字。他們應該從一封信開始,然後是一個數字。所以你需要遵循一個不同的命名約定爲你的ID。

我不知道如果這是你問什麼,但你可以得到第一行的id像這樣的東西:

$("table tr:first").get(0).id