【问题标题】:Ajax pagination with Django and Jquery使用 Django 和 Jquery 进行 Ajax 分页
【发布时间】:2014-08-30 18:25:54
【问题描述】:

我正在我的应用程序中构建一个简单的搜索引擎,以根据特定条件获得一些结果。我想使用 ajax 和 jquery 来实现使用 ajax 的搜索并对结果进行分页。 Ajax 可以很好地进行搜索,但是当我尝试对结果进行分页时,它仅在我第一次点击 prev 或 next 时才有效,而第二次它似乎中断了。

(function(){

$("form#search").on("submit", function(event){

    event.preventDefault();
    var $this = $(this);


    $.ajax({

        type:"GET",
        dataType:"html",
        data:$this.serialize(),
        url:"/properties/search/",
        success:function(resp){

            $(".col-md-9").html($(resp).find(".properties"));

        },
        error: function(){

            $(".col-md-9").html('<h1>Verifique los datos ingresados para realizar una correcta búsqueda</h1>');

        }

    })

})

})();

//分页

(function(){

$("ul.pagination li a").on("click", function(event){

    $this = $(this);
    console.log($this.prop("href"));
    event.preventDefault();

    $.ajax({

        type:"GET",
        dataType:"html",
        url:$this.prop("href"),
        success:function(resp){

            $(".col-md-9").html($(resp).find(".properties"));

        },
        error: function(){

            $(".col-md-9").html('<h1>Verifique los datos ingresados para realizar una correcta búsqueda</h1>');

        }

    })

})

})();

// 模板

       <div class="text-center">
                    <ul class="pagination">
                        <li><a href="/properties/search">&lt;&lt;</a></li>
                        {% if properties.has_previous %}
                            <li><a href="?{{ path }}&amp;page={{ properties.previous_page_number }}">Anterior</a></li>
                        {% else %}
                            <li><a href="#">Anterior</a></li>
                        {% endif %}
                        {% if properties.has_next %}
                            <li><a href="?{{ path }}&amp;page={{ properties.next_page_number }}">Siguiente</a></li>
                        {% else %}
                            <li><a href="#">Siguiente</a></li>
                        {% endif %}
                        <li><a href="?{{ path }}&amp;page={{ properties.paginator.num_pages }}">&gt;&gt;</a></li>
                    </ul>
                </div>

【问题讨论】:

  • 控制台有错误吗?

标签: jquery ajax django


【解决方案1】:

我使用 $(document).on("click", "ul.pagination li a", function(event){}) 解决了这个问题

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2016-01-28
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多