开发者

jQuery script doesn't work in safari when button is disabled

开发者 https://www.devze.com 2022-12-09 12:45 出处:网络
I wrote this simple piece of code to prevent a form from being submitted more than once. jQuery.fn.preventDoubleSubmit = function() {

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.beenSu开发者_开发百科bmitted = 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号