Ajax过滤器无法正常工作

时间:2016-09-19 07:56:29

标签: javascript php jquery ajax

我已经使用AJAX在我的网站上创建了一个过滤器。当我使用点击事件调用AJAX时,间隔很好。但是,当我同时点击“提交”按钮多次时,我在页面上得到了重复的结果我不是在这里给出全部细节。

我在div计数的基础上将结果附加到我的页面中。这就是我想的问题。

if ($("#holiday-list-loaded-view-start").nextAll("div").first().hasClass('hotel-list-view')) {
    $(".hotel-list-view").last().after('<div class="hotel-list-view"><div class="wrapper"><div class="col-md-4 col-sm-6 switch-img clear-padding"><a href="'+base_url+'package_detail/view_detail/'+value["id"]+'"><img src="'+img+'"alt="cruise"></a></div><div class="col-md-6 col-sm-6 hotel-info"><div><div class="hotel-header"><h5>'+value["package_name"].substr(0,120)+'<!--<span><i class="fa fa-star colored"></i><i class="fa fa-star colored"></i><i class="fa fa-star colored"></i><i class="fa fa-star colored"></i><i class="fa fa-star-o colored"></i></span>--></h5><p class="small"><i class="fa fa-map-marker"></i>'+shortest_itinerary+'&nbsp<span  id="packagepopup'+value['id']+'" class="package_modal_details" ><i class="fa fa-info-circle" title="View Details" ></i></span><!--<i class="fa fa-phone"></i>123456789--></p></div><div class="hotel-header"><p>Inclusions</p></div><div class="hotel-desc"><p>'+inclusions+'</p></div><!--<div class="hotel-header"><p>Themes</p></div><div class="hotel-desc"><p>'+themes+'</p></div>--></div></div><div class="clearfix visible-sm-block"></div><div class="col-md-2 rating-price-box text-center clear-padding"><div class="rating-box"><div class=""><strong>Themes</strong></div><div class="small">'+themes+'</div></div><div class="room-book-box"><div class="price"><h5>Rs '+value["package_price"]+'/Person</h5></div><div class="book"><a href="#" class="send_package_query" id="packagequery'+value["id"]+'" title="Send a Query">QUERY</a><a href="'+base_url+'package_detail/view_detail/'+value["id"]+'" title="View Detail">DETAIL</a></div></div></div></div></div>'); 
} else {
    $("#holiday-list-loaded-view-start").after('<div class="hotel-list-view"><div class="wrapper"><div class="col-md-4 col-sm-6 switch-img clear-padding"><a href="'+base_url+'package_detail/view_detail/'+value["id"]+'"><img src="'+img+'"alt="cruise"></a></div><div class="col-md-6 col-sm-6 hotel-info"><div><div class="hotel-header"><h5>'+value["package_name"].substr(0,120)+'<!--<span><i class="fa fa-star colored"></i><i class="fa fa-star colored"></i><i class="fa fa-star colored"></i><i class="fa fa-star colored"></i><i class="fa fa-star-o colored"></i></span>--></h5><p class="small"><i class="fa fa-map-marker"></i>'+shortest_itinerary+'<span  id="packagepopup'+value['id']+'" class="package_modal_details" ><i class="fa fa-info-circle" title="View Details" ></i></span><!--<i class="fa fa-phone"></i>123456789--></p></div><div class="hotel-header"><p>Inclusions</p></div><div class="hotel-desc"><p>'+inclusions+'</p></div><!--<div class="hotel-header"><p>Themes</p></div><div class="hotel-desc"><p>'+themes+'</p></div>--></div></div><div class="clearfix visible-sm-block"></div><div class="col-md-2 rating-price-box text-center clear-padding"><div class="rating-box"><div class=""><strong>Themes</strong></div><div class="small">'+themes+'</div></div><div class="room-book-box"><div class="price"><h5>Rs '+value["package_price"]+'/Person</h5></div><div class="book"> <a href="#" class="send_package_query" id="packagequery'+value["id"]+'" title="Send a Query">QUERY</a><a href="'+base_url+'package_detail/view_detail/'+value["id"]+'" title="View Detail">DETAIL</a></div></div></div></div></div>'); 
}

哦,是的,当页面被渲染时,我得到它。如果发生另一个过滤事件则需要时间平均值然后由于div元素的不正确匹配,它的结果也附加到同一个块,因此结果重复。

但如何解决它。我不知道。

1 个答案:

答案 0 :(得分:0)

然后使用empty()函数清除以前的结果(可能的位置就在循环开始之前),

//this will erase previous result
$(".hotel-list-view").empty();
// or 
 $("#holiday-list-loaded-view-start").empty();
// then append function may be before/after/append