unbind jQuery doesn't work

Trying to unbind a div so after it has been clicked it will not work until i bind it again, but it doesn't unbind, what i'm doing wrong?

$('#envio').click(function() {
  $('#mensagemenvio').html('<span style="color: blue; font-size: 10px">Enviando</span>');
  $(this).unbind('click');

  $('#saque').submit(function (){
    $.ajax({
      type: 'POST',
      url: 'enviar.php',
      data: $('#sacar_btc').serialize(),
      success: function (e){
        $('#mensagemenvio').html(e);
      }
    }); 
    return false;
  });
});
<div style="width: calc(70% - 60px); height: 25px; line-height: 25px;" class="envia" id="envio">
  <input id="envia" class="submit" type="submit" value="Confirmar" name="command"/>
</div>

I've tried with off() instead of unbind(), and also tried changing my jQuery version.

What i want is to disable the submit button while the php script is running, it is a slow script that takes about 5 seconds to finish, so user cant submit it twice.

Upvotes: 0

Views: 165

Answers (2)

Claudio
Claudio

Reputation: 5203

You can just add a flag to indicate that the something is being submitted like this:

window.isSubmitting = false;

$('#envio').click(function() {
  if (window.isSubmitting) {
     return false;
  } else {
     window.isSubmitting = true;
  }

  $('#mensagemenvio').html('<span style="color: blue; font-size: 10px">Enviando</span>');


  $('#saque').submit(function (){
    $.ajax({
      type: 'POST',
      url: 'enviar.php',
      data: $('#sacar_btc').serialize(),
      success: function (e){
        window.isSubmitting = false;
        $('#mensagemenvio').html(e);
      },
      error: function() { // important the error handler to enable the event
        window.isSubmitting = false;
      }      
    }); 
    return false;
  });
});

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160191

In the click handler you bind a function to run on form submission.

You do not un-bind that form submit handler.

I suspect what you really want to do is create a click handler that runs the Ajax, not that binds a handler to form submission. Then you could unbind that click-handler and it would do what you intended.

Upvotes: 1

Related Questions