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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.