【发布时间】:2019-02-17 14:32:32
【问题描述】:
每当通过按 Enter 提交搜索表单时,我都会使用 AJAX 调用来获取我的轮播项目。当按下 Enter 时,我想删除以前的项目并在轮播中添加新项目。
它一直都很好用,除了重复按 Enter 时,它不会删除旧项目,只是继续添加新项目。
删除项目
function removeResult() {
var i = 0;
$("#result_section").slideUp(750, function () {
$('.result_item').each(function(){
$(".prof-carousel").trigger('remove.owl.carousel', [i++])
.trigger('refresh.owl.carousel');
});
});
}
添加项目
$("#index_search_btn").click(function (e) {
e.preventDefault();
removeResult();
var formData = new FormData();
formData.append('type', search_type);
formData.append('q', search_q);
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: "/search",
data: formData,
success: function (response) {
$("#result_section").slideDown(750, function () {
$.each(response, function (index, prof) {
var item = '';
item += '<div class="item carousel_ostad_item result_item">'+ 'whatever...'+'</div>';
$('.my-carousel').owlCarousel().trigger('add.owl.carousel',[jQuery(item)]).trigger('refresh.owl.carousel');
});
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
}
});
});
【问题讨论】:
标签: javascript jquery ajax owl-carousel