JQuery Allow only one click before .ajax ()

I try to allow a button to click only once, and then some data is passed through ajax. The problem I am facing is that the user can click 50x and the POST data is sent every time?

jQuery("#id").unbind('click');

jQuery.ajax({
    type: "POST",
    url: ajax_url,
    data: ajax_data,
    cache: false,
    success: function (html) {
        location.reload(true);
    }
});

How can I make sure that if the user clicks #ID100x - that the data is sent only once? And then #ID is on again?

+3
source share
7 answers

just turn off the button

$("#id").attr("disabled", "disabled")

and then turn it on in the success function

$("#id").removeAttr("disabled")
+5
source

You can use the function .one()in jQuery.

jQuery("#id").one('click', function()
{
    jQuery.ajax({
        type: "POST",
        url: ajax_url,
        data: ajax_data,
        cache: false,
        success: function (html) {
            location.reload(true);
        }
    });
});

, click, ajax, .

+8

- , reset :

if(clicked == False){
    clicked = True;
    jQuery("#id").unbind('click');

    jQuery.ajax({
       type: "POST",
       url: ajax_url,
       data: ajax_data,
       cache: false,
       success: function (html) {
           location.reload(true);
           clicked = False;
       },
       error: function () {
            alert("Error happened");
           clicked = False;
       }
    });
}
+2
0

, ajax .

0

click , ajax.

- , , , , , , ajax, . ajax , .

0

:

$(document).ajaxStart({ function() {
 $('#submit_button').click(function(){
   return false;
 });
});

where: #submit_button is the identifier of the U element to disable

this code will be disabled by clicking the submit button

0
source

All Articles