重复提交搜索表单时添加和删除猫头鹰的食物

时间:2019-02-17 14:32:33

标签: javascript jquery ajax owl-carousel

每当通过按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);

         }

    });

});

2 个答案:

答案 0 :(得分:0)

可能是因为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);
        }

    });

});

答案 1 :(得分:0)

我确实提出了一个简单的解决方案。 按下搜索按钮后,我禁用了搜索按钮(因此该表单无法重复提交),并在成功的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);
    }

});

});