【发布时间】: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"><<</a></li>
{% if properties.has_previous %}
<li><a href="?{{ path }}&page={{ properties.previous_page_number }}">Anterior</a></li>
{% else %}
<li><a href="#">Anterior</a></li>
{% endif %}
{% if properties.has_next %}
<li><a href="?{{ path }}&page={{ properties.next_page_number }}">Siguiente</a></li>
{% else %}
<li><a href="#">Siguiente</a></li>
{% endif %}
<li><a href="?{{ path }}&page={{ properties.paginator.num_pages }}">>></a></li>
</ul>
</div>
【问题讨论】:
-
控制台有错误吗?