在重复提交搜索表单时添加和删除 owl 个 carousal 项目
Add and remove owl carousal items when search form is submitted repeatedly
每当按 Enter 键提交搜索表单时,我都会使用 AJAX 调用来获取我的 carousal 项目。我想在按下 Enter 时删除以前的项目并向 carousal 添加新项目。
除了重复按 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);
}
});
});
可能是因为 Ajax 是异步的并且更慢并且会发生类似于以下的事情:
Enter -> Remove (old el) -> Ajax call 1 -> Enter -> Remove (nothing) -> Ajax call 2 -> Ajax callback 1 ADD -> Ajax callback 2 ADD
我希望它是清楚的,我不知道如何表达这个。
你可以做的是删除 success
Ajax 回调中的旧元素。
$("#index_search_btn").click(function (e) {
e.preventDefault();
// #### Remove this...
//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) {
// ... and move here. ####
removeResult();
$("#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);
}
});
});
我确实想出了一个简单的解决方案。
我在按下搜索按钮时禁用它(因此无法重复提交表单)并在成功添加狂欢项目后再次启用它 Ajax 回调。
$("#index_search_btn").click(function (e) {
e.preventDefault();
///// disable search button /////
$("#index_search_btn").attr("disabled", true);
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');
///// enable search button /////
$("#index_search_btn").removeAttr("disabled");
});
});
}, error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
}
});
});
每当按 Enter 键提交搜索表单时,我都会使用 AJAX 调用来获取我的 carousal 项目。我想在按下 Enter 时删除以前的项目并向 carousal 添加新项目。
除了重复按 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);
}
});
});
可能是因为 Ajax 是异步的并且更慢并且会发生类似于以下的事情:
Enter -> Remove (old el) -> Ajax call 1 -> Enter -> Remove (nothing) -> Ajax call 2 -> Ajax callback 1 ADD -> Ajax callback 2 ADD
我希望它是清楚的,我不知道如何表达这个。
你可以做的是删除 success
Ajax 回调中的旧元素。
$("#index_search_btn").click(function (e) {
e.preventDefault();
// #### Remove this...
//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) {
// ... and move here. ####
removeResult();
$("#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);
}
});
});
我确实想出了一个简单的解决方案。 我在按下搜索按钮时禁用它(因此无法重复提交表单)并在成功添加狂欢项目后再次启用它 Ajax 回调。
$("#index_search_btn").click(function (e) {
e.preventDefault();
///// disable search button /////
$("#index_search_btn").attr("disabled", true);
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');
///// enable search button /////
$("#index_search_btn").removeAttr("disabled");
});
});
}, error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
}
});
});