【问题标题】:Multiple ajax content requests in a loop循环中的多个 ajax 内容请求
【发布时间】:2010-12-14 05:01:09
【问题描述】:

我正在尝试将一些 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 返回最后一行

【问题讨论】:

    标签: javascript jquery ajax while-loop


    【解决方案1】:

    您需要将 xmlHttps 推送到数组或抽象数据类型中,然后您可以附加事件处理程序。似乎 jquery 不适合你。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我认为:

        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);
                        }       
                    }
                }
            });                     
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-09
          • 2016-10-15
          • 1970-01-01
          • 2019-03-24
          • 1970-01-01
          • 1970-01-01
          • 2017-08-08
          • 1970-01-01
          相关资源
          最近更新 更多