0

我正在尝试将一些 ajax 内容加载到表中,不幸的是它只是多次加载最后一行,而不是加载每个新行。

这是我正在使用的代码:

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

        success: function(msg){
            var newid = msg;
            current = $("#list tr:first").get(0).id;
            if(newid != current){
                while (current<newid)
                {
                    current++;
                    addToList(current);
                }   
            }
        }
    });                 
}

function addToList(x)
{
     $.ajax({
            type: 'GET',
            url: 'include/ajaxActions.php',
            data: "action=displayRow&row="+x,

            success: function(msg){
                $("#list").prepend(msg);
                $("#list tr:first").highlightFade({
                    speed:3000
                });
                lastrow = x-20;
                $('#'+lastrow).remove();
            }
     });

}

displayLastEvent返回最后一行的 id。

displayRow返回最后一行

4

3 回答 3

1

您需要将 xmlHttps 推送到数组或抽象数据类型中,然后您可以附加事件处理程序。似乎 jquery 并没有为你做到这一点。

于 2009-11-07T17:42:49.453 回答
1

我将通过鼓励您更改方法来解决此问题,因为每当需要将更多行添加到列表(2+ 而不是 2)时,您可能会发出比必要更多的 AJAX 请求。

我会更新include/ajaxActions.php?action=displayRow以接受 CSV 的 id(或您传入的任何内容),并返回一组行数据,而不是仅一行的数据。

于 2009-11-07T22:19:37.457 回答
0

我觉得:

current = $("#list tr:first").get(0).id;

jQuery返回始终与仅记住第一次加载页面时相同的结果。
例如,如果您有一个tr[id=0]

pass 1 : current = 0; msg = 1 -> 1 tr prepended with id = 1;
pass 2 : current is always 0 (not 1); msg = 1 -> 1 tr prepended with id = 1;
...

您应该做的是jQuery在添加消息后识别您的页面结构,或者以不同的方式存储最后一个索引:使用hidden input例如:

HTML:

<input type="hidden" value="0" id="lastId"/>

脚本:

#lastId value加载页面时初始化:

$(document).ready(function(){
    $("#lastId").val(0);//or by using your displayLastEvent action
});

修改periodicRefresh#lastId value

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

        success: function(msg){
                var newid = msg;
                var current = $("#lastId").val();
                if(current<newid) $("#lastId").val(newid);
            if(newid != current){
                while (current<newid)
                {
                        current++;
                        addToList(current);
                }       
            }
        }
    });                     
}
于 2009-11-12T16:17:36.513 回答