locked
How to prevent multiple click in JQuery RRS feed

  • Question

  • User82362805 posted

    I am getting data from server through ajax call on button click but the problem is when I click the button multiple times there is multiple ajax call occurred how could I stop that

    here is my code

    Connection Timeout=30
    Procedure or function 'SPGetBookingByTokenID' expects parameter '@TokenID', which was not supplied.

    Tri@001dent002

    <span id="loader"><img src="~/assets/images/loading.gif" width="50" height="50" /></span>

    $("#btnaddTender").on('click', function (e) {

    $('.listboxclass').empty();
    $(".tendercatddl")
    $("#catloader").show();
    e.preventDefault();
    var parentId = $("#addtendercat").val();

    $.ajax({
    url: "@Url.Action("PostCategory", "Post")?PostTypeid=" + '@PostTypeId' + "&postType=" + postType + "&ParentId=" + parentId,
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
    $("#catloader").hide();
    if (data != 2 && data != 1) {
    $(".listboxclass").append('<option value="' + data[0].postTypeId + '">' + data[0].name + '</option>');
    RefreshTenderListbox();
    $("#CategoryModel").modal("hide");
    $("#CategoryMsg").addClass("alert success").text("Your category has been added successfully kindly select your multiple desired ctegories from list");
    }
    $("#postType").val("");

    if (data == '2') {
    $("#message").text("Category already exist choose another one");
    }
    //$(this).unbind(e);
    },
    error: function (err) {
    alert('error');
    }
    });
    })

    Monday, October 1, 2018 1:26 PM

Answers

  • User-369506445 posted

    hi

    you can disable the button when Ajax <g class="gr_ gr_33 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="33" data-gr-id="33">is start</g> and remove it when Ajax is complete below like :

    <script>
    
        $("#btnaddTender").on('click', function (e) {
    
            $('.listboxclass').empty();
            $(".tendercatddl")
            $("#catloader").show();
            e.preventDefault();
            var parentId = $("#addtendercat").val();
    
            $('#btnaddTender').attr('disabled', 'disabled');
    
    
            $.jquer({
                url: "@Url.Action("PostCategory", "Post")?PostTypeid=" + '@PostTypeId' + "&postType=" + postType + "&ParentId=" + parentId,
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $("#catloader").hide();
                    if (data != 2 && data != 1) {
                        $(".listboxclass").append('<option value="' + data[0].postTypeId + '">' + data[0].name + '</option>');
                        RefreshTenderListbox();
                        $("#CategoryModel").modal("hide");
                        $("#CategoryMsg").addClass("alert success").text("Your category has been added successfully kindly select your multiple desired ctegories from list");
                    }
                    $("#postType").val("");
    
                    if (data == '2') {
                        $("#message").text("Category already exist choose another one");
                    }
                    //$(this).unbind(e);
                },
                error: function (err) {
                    alert('error');
                },
                oncomplete: function ()
                {
                    $('#btnaddTender').removeAttr('disabled');
                }
            });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 1, 2018 2:00 PM