views:

272

answers:

1

I wrote this simple piece of code to prevent a form from being submitted more than once.

jQuery.fn.preventDoubleSubmit = function() {

  this.find(":submit").each(function() {
    $(this).click(function() {
      this.originalValue = this.value;
      this.disabled = true;
      this.beenClicked = true;
    });
  });

  // [...]

  return this.submit(function() {
    if (this.beenSubmitted) {
      return false;
    } else {
      this.beenSubmitted = true;
    }
  });

};

// [...]

Unfortunately, with Safari it doesn't work. When I click the form the button is disabled and Safari doesn't submit the form.

Any idea?

+2  A: 

You know you don't have to click the button for a form to submit, right? I think you should take a whole new approach, accounting for this. To prevent double submission, simply:

$(".selector").submit(window.mySubmitHandler);

function mySubmitHandler(evt) {
    $("#submitButton").attr("disabled", true);
    //do other stuff here if you want

    //redefine the function in the function itself
    window.mySubmitHandler = function(evt) {
        evt.preventDefault();
    }

    return true;
}

This way, the function will allow the submission the first time and overwrite itself. Then every subsequent attempt will simply stop the default behavior of the event.

geowa4