简体   繁体   English

防止在 jQuery 中重复提交表单

[英]Prevent double submission of forms in jQuery

I have a form that takes a little while for the server to process.我有一个需要服务器处理一段时间的表单。 I need to ensure that the user waits and does not attempt to resubmit the form by clicking the button again.我需要确保用户等待并且不会尝试通过再次单击按钮来重新提交表单。 I tried using the following jQuery code:我尝试使用以下 jQuery 代码:

<script type="text/javascript">
$(document).ready(function() {
    $("form#my_form").submit(function() {
        $('input').attr('disabled', 'disabled');
        $('a').attr('disabled', 'disabled');
        return true;
    });
});
</script>

When I try this in Firefox everything gets disabled but the form is not submitted with any of the POST data it is supposed to include.当我在 Firefox 中尝试此操作时,所有内容都被禁用,但表单未与它应该包含的任何 POST 数据一起提交。 I can't use jQuery to submit the form because I need the button to be submitted with the form as there are multiple submit buttons and I determine which was used by which one's value is included in the POST.我无法使用 jQuery 提交表单,因为我需要将按钮与表单一起提交,因为有多个提交按钮,并且我确定 POST 中包含哪个值。 I need the form to be submitted as it usually is and I need to disable everything right after that happens.我需要像往常一样提交表单,并且我需要在发生这种情况后立即禁用所有内容。

Thanks!谢谢!

Update in 2018 : I just got some points for this old answer, and just wanted to add that the best solution would be to make the operation idempotent so that duplicate submissions are harmless. 2018 年更新:我刚刚从这个旧答案中得到了一些分数,只是想补充一点,最好的解决方案是使操作具有幂等性,以便重复提交是无害的。

Eg, if the form creates an order, put a unique ID in the form.例如,如果表单创建了一个订单,则在表单中放置一个唯一的 ID。 The first time the server sees an order creation request with that id, it should create it and respond "success".服务器第一次看到带有该 id 的订单创建请求时,它应该创建它并响应“成功”。 Subsequent submissions should also respond "success" (in case the client didn't get the first response) but shouldn't change anything.随后的提交也应该响应“成功”(以防客户没有得到第一个响应)但不应该改变任何东西。

Duplicates should be detected via a uniqueness check in the database to prevent race conditions.应通过数据库中的唯一性检查来检测重复项,以防止出现竞争条件。


I think that your problem is this line:我认为你的问题是这一行:

$('input').attr('disabled','disabled');

You're disabling ALL the inputs, including, I'd guess, the ones whose data the form is supposed to submit.您正在禁用所有输入,包括,我猜,表单应该提交其数据的输入。

To disable just the submit button(s), you could do this:要仅禁用提交按钮,您可以这样做:

$('button[type=submit], input[type=submit]').prop('disabled',true);

However, I don't think IE will submit the form if even those buttons are disabled.但是,我认为即使这些按钮被禁用,IE 也不会提交表单。 I'd suggest a different approach.我建议采用不同的方法。

A jQuery plugin to solve it一个jQuery插件来解决它

We just solved this problem with the following code.我们刚刚用下面的代码解决了这个问题。 The trick here is using jQuery's data() to mark the form as already submitted or not.这里的技巧是使用 jQuery 的data()来标记表单是否已经提交。 That way, we don't have to mess with the submit buttons, which freaks IE out.这样,我们就不必弄乱提交按钮,这让 IE 感到害怕。

// jQuery plugin to prevent double submission of forms
jQuery.fn.preventDoubleSubmission = function() {
  $(this).on('submit',function(e){
    var $form = $(this);

    if ($form.data('submitted') === true) {
      // Previously submitted - don't submit again
      e.preventDefault();
    } else {
      // Mark it so that the next submit can be ignored
      $form.data('submitted', true);
    }
  });

  // Keep chainability
  return this;
};

Use it like this:像这样使用它:

$('form').preventDoubleSubmission();

If there are AJAX forms that should be allowed to submit multiple times per page load, you can give them a class indicating that, then exclude them from your selector like this:如果有应该允许每次页面加载多次提交的 AJAX 表单,您可以给它们一个指示这一点的类,然后将它们从您的选择器中排除,如下所示:

$('form:not(.js-allow-double-submission)').preventDoubleSubmission();

Timing approach is wrong - how do you know how long the action will take on client's browser?计时方法是错误的 - 你怎么知道客户端浏览器上的操作需要多长时间?

How to do it怎么做

$('form').submit(function(){
  $(this).find(':submit').attr('disabled','disabled');
});

When form is submitted it will disable all submit buttons inside.提交表单时,它将禁用其中的所有提交按钮。

Remember, in Firefox when you disable a button this state will be remembered when you go back in history.请记住,在 Firefox 中,当您禁用按钮时,该状态将在您返回历史记录时被记住。 To prevent that you have to enable buttons on page load, for example.例如,为了防止您必须在页面加载时启用按钮。

I think Nathan Long's answer is the way to go.我认为 Nathan Long 的答案是要走的路。 For me, I am using client-side validation, so I just added a condition that the form be valid.对我来说,我使用的是客户端验证,所以我只是添加了表单有效的条件。

EDIT : If this is not added, the user will never be able to submit the form if the client-side validation encounters an error.编辑:如果没有添加,如果客户端验证遇到错误,用户将永远无法提交表单。

        // jQuery plugin to prevent double submission of forms
        jQuery.fn.preventDoubleSubmission = function () {
            $(this).on('submit', function (e) {
                var $form = $(this);

                if ($form.data('submitted') === true) {
                    // Previously submitted - don't submit again
                    alert('Form already submitted. Please wait.');
                    e.preventDefault();
                } else {
                    // Mark it so that the next submit can be ignored
                    // ADDED requirement that form be valid
                    if($form.valid()) {
                        $form.data('submitted', true);
                    }
                }
            });

            // Keep chainability
            return this;
        };

event.timeStamp doesn't work in Firefox. event.timeStamp在 Firefox 中不起作用。 Returning false is non-standard, you should call event.preventDefault() .返回 false 是非标准的,您应该调用event.preventDefault() And while we're at it, always use braces with a control construct .当我们这样做时,请始终使用带有控制结构的大括号

To sum up all of the previous answers, here is a plugin that does the job and works cross-browser.总结所有以前的答案,这里有一个插件可以完成工作并跨浏览器工作。

jQuery.fn.preventDoubleSubmission = function() {

    var last_clicked, time_since_clicked;

    jQuery(this).bind('submit', function(event) {

        if(last_clicked) {
            time_since_clicked = jQuery.now() - last_clicked;
        }

        last_clicked = jQuery.now();

        if(time_since_clicked < 2000) {
            // Blocking form submit because it was too soon after the last submit.
            event.preventDefault();
        }

        return true;
    });
};

To address Kern3l, the timing method works for me simply because we're trying to stop a double-click of the submit button.为了解决 Kern3l,计时方法对我有用,因为我们试图停止双击提交按钮。 If you have a very long response time to a submission, I recommend replacing the submit button or form with a spinner.如果您对提交的响应时间很长,我建议用微调器替换提交按钮或表单。

Completely blocking subsequent submissions of the form, as most of the above examples do, has one bad side-effect: if there is a network failure and they want to try to resubmit, they would be unable to do so and would lose the changes they made.完全阻止表单的后续提交,就像上面的大多数例子一样,有一个不好的副作用:如果出现网络故障并且他们想尝试重新提交,他们将无法这样做并且会丢失他们所做的更改制作。 This would definitely make an angry user.这肯定会让用户生气。

Please, check out jquery-safeform plugin.请查看jquery-safeform插件。

Usage example:用法示例:

$('.safeform').safeform({
    timeout: 5000,  // disable next submission for 5 sec
    submit: function() {
        // You can put validation and ajax stuff here...

        // When done no need to wait for timeout, re-enable the form ASAP
        $(this).safeform('complete');
        return false;
    }
});

...but the form is not submitted with any of the POST data it is supposed to include. ...但该表单没有提交它应该包含的任何 POST 数据。

Correct.正确的。 Disabled form element names/values will not be sent to the server.禁用的表单元素名称/值将不会发送到服务器。 You should set them as readonly elements.您应该将它们设置为只读元素。

Also, anchors cannot be disabled like that.此外,锚不能像那样被禁用。 You will need to either remove their HREFs (not recommended) or prevent their default behaviour (better way), eg:您将需要删除他们的 HREF(不推荐)或阻止他们的默认行为(更好的方式),例如:

<script type="text/javascript">
$(document).ready(function(){
    $("form#my_form").submit(function(){
      $('input').attr('readonly', true);
      $('input[type=submit]').attr("disabled", "disabled");
      $('a').unbind("click").click(function(e) {
          e.preventDefault();
          // or return false;
      });
    });
</script>

Nathan's code but for jQuery Validate plugin Nathan 的代码,但用于 jQuery Validate 插件

If you happen to use jQuery Validate plugin, they already have submit handler implemented, and in that case there is no reason to implement more than one.如果您碰巧使用 jQuery Validate 插件,它们已经实现了提交处理程序,在这种情况下,没有理由实现多个。 The code:编码:

jQuery.validator.setDefaults({
  submitHandler: function(form){
    // Prevent double submit
    if($(form).data('submitted')===true){
      // Previously submitted - don't submit again
      return false;
    } else {
      // Mark form as 'submitted' so that the next submit can be ignored
      $(form).data('submitted', true);
      return true;
    }
  }
});

You can easily expand it within the } else { -block to disable inputs and/or submit button.您可以在} else { -block 中轻松扩展它以禁用输入和/或提交按钮。

Cheers干杯

There is a possibility to improve Nathan Long's approach .有可能改进 Nathan Long 的方法 You can replace the logic for detection of already submitted form with this one:您可以用这个替换检测已提交表单的逻辑:

var lastTime = $(this).data("lastSubmitTime");
if (lastTime && typeof lastTime === "object") {
    var now = new Date();
    if ((now - lastTime) > 2000) // 2000ms
        return true;
    else
        return false;
}
$(this).data("lastSubmitTime", new Date());
return true; // or do an ajax call or smth else

I ended up using ideas from this post to come up with a solution that is pretty similar to AtZako's version.我最终使用这篇文章中的想法提出了一个与 AtZako 版本非常相似的解决方案。

 jQuery.fn.preventDoubleSubmission = function() {

    var last_clicked, time_since_clicked;

    $(this).bind('submit', function(event){

    if(last_clicked) 
      time_since_clicked = event.timeStamp - last_clicked;

    last_clicked = event.timeStamp;

    if(time_since_clicked < 2000)
      return false;

    return true;
  });   
};

Using like this:像这样使用:

$('#my-form').preventDoubleSubmission();

I found that the solutions that didn't include some kind of timeout but just disabled submission or disabled form elements caused problems because once the lock-out is triggered you can't submit again until you refresh the page.我发现不包含某种超时但仅禁用提交或禁用表单元素的解决方案会导致问题,因为一旦触发锁定,您就无法再次提交,直到刷新页面。 That causes some problems for me when doing ajax stuff.在做 ajax 的时候,这给我带来了一些问题。

This can probably be prettied up a bit as its not that fancy.这可能可以美化一点,因为它不是那么花哨。

If using AJAX to post a form, set async: false should prevent additional submits before the form clears:如果使用AJAX发布表单,设置async: false应该可以防止在表单清除之前额外提交:

$("#form").submit(function(){
    var one = $("#one").val();
    var two = $("#two").val();
    $.ajax({
      type: "POST",
      async: false,  // <------ Will complete submit before allowing further action
      url: "process.php",
      data: "one="+one+"&two="+two+"&add=true",
      success: function(result){
        console.log(result);
        // do something with result
      },
      error: function(){alert('Error!')}
    });
    return false;
   }
});

Modified Nathan's solution a little for Bootstrap 3. This will set a loading text to the submit button.针对 Bootstrap 3 稍微修改了 Nathan 的解决方案。这将为提交按钮设置加载文本。 In addition it will timeout after 30 seconds and allow the form to be resubmitted.此外,它将在 30 秒后超时并允许重新提交表单。

jQuery.fn.preventDoubleSubmission = function() {
  $('input[type="submit"]').data('loading-text', 'Loading...');

  $(this).on('submit',function(e){
    var $form = $(this);

    $('input[type="submit"]', $form).button('loading');

    if ($form.data('submitted') === true) {
      // Previously submitted - don't submit again
      e.preventDefault();
    } else {
      // Mark it so that the next submit can be ignored
      $form.data('submitted', true);
      $form.setFormTimeout();
    }
  });

  // Keep chainability
  return this;
};

jQuery.fn.setFormTimeout = function() {
  var $form = $(this);
  setTimeout(function() {
    $('input[type="submit"]', $form).button('reset');
    alert('Form failed to submit within 30 seconds');
  }, 30000);
};

Use two submit buttons.使用两个提交按钮。

<input id="sub" name="sub" type="submit" value="OK, Save">
<input id="sub2" name="sub2" type="submit" value="Hidden Submit" style="display:none">

And jQuery:和 jQuery:

$("#sub").click(function(){
  $(this).val("Please wait..");
  $(this).attr("disabled","disabled");
  $("#sub2").click();
});

Use simple counter on submit.在提交时使用简单的计数器。

    var submitCounter = 0;
    function monitor() {
        submitCounter++;
        if (submitCounter < 2) {
            console.log('Submitted. Attempt: ' + submitCounter);
            return true;
        }
        console.log('Not Submitted. Attempt: ' + submitCounter);
        return false;
    }

And call monitor() function on submit the form.并在提交表单时调用monitor()函数。

    <form action="/someAction.go" onsubmit="return monitor();" method="POST">
        ....
        <input type="submit" value="Save Data">
    </form>

I've been having similar issues and my solution(s) are as follows.我一直有类似的问题,我的解决方案如下。

If you don't have any client side validation then you can simply use the jquery one() method as documented here.如果您没有任何客户端验证,那么您可以简单地使用此处记录的 jquery one() 方法。

http://api.jquery.com/one/ http://api.jquery.com/one/

This disables the handler after its been invoked.这将在调用后禁用处理程序。

$("#mysavebuttonid").on("click", function () {
  $('form').submit();
});

If you're doing client side validation as I was doing then its slightly more tricky.如果您像我一样进行客户端验证,那么它稍微有点棘手。 The above example would not let you submit again after failed validation.上面的例子不会让你在验证失败后再次提交。 Try this approach instead试试这个方法

$("#mysavebuttonid").on("click", function (event) {
  $('form').submit();
  if (boolFormPassedClientSideValidation) {
        //form has passed client side validation and is going to be saved
        //now disable this button from future presses
        $(this).off(event);
   }
});

here is how I do it: 我是这样做的:

$(document).ready(function () {
  $('.class_name').click(function () {
    $(this).parent().append('<img src="data:image/gif;base64,R0lGODlhEAAQAPQAAP///wAAAPDw8IqKiuDg4EZGRnp6egAAAFhYWCQkJKysrL6+vhQUFJycnAQEBDY2NmhoaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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FeSAgAgIpnOSoLgxxvqgKLEcCC65KEAByKK8cSpA4DAiHQ/DkKhGKh4ZCtCyZGo6F6iYYPAqFgYy02xkSaLEMV34tELyRYNEsCQyHlvWkGCzsPgMCEAY7Cg04Uk48LAsDhRA8MVQPEF0GAgqYYwSRlycNcWskCkApIyEAOwAAAAAAAAAAAA==" />');
    $(this).hide();
  });
});

credits: https://github.com/phpawy/jquery-submit-once 学分: https//github.com/phpawy/jquery-submit-once

You can stop the second submit by this您可以通过此停止第二次提交

$("form").submit(function() {
        // submit more than once return false
        $(this).submit(function() {
            return false;
        });
        // submit once return true
        return true; // or do what you want to do
    });
});

All of these solutions are passable as long as you're not importing the jQuery validation plugin.只要您不导入 jQuery 验证插件,所有这些解决方案都可以通过。

For example, if the client enters invalid input and submits the form, the button will become disabled regardless of whether jQuery validation detects the invalid input.例如,如果客户端输入无效输入并提交表单,则无论 jQuery 验证是否检测到无效输入,按钮都会被禁用。 Then the client won't be able to re-submit the form after fixing their input.然后客户将无法在修复他们的输入后重新提交表单。

The workaround for this issue only took one if statement:此问题的解决方法只需要一个 if 语句:

$('form').submit(function () {
    if ($(this).valid()) {
        $(this).find("input[type='submit']").prop('disabled', true);
    }
});

After adding this code to my globally-imported JavaScript file, I was unsuccessful in trying to double-post forms on my website.将此代码添加到我全局导入的 JavaScript 文件后,我尝试在我的网站上重复发布表单失败。

this code will display loading on the button label, and set button to此代码将在按钮标签上显示加载,并将按钮设置为

disable state, then after processing, re-enable and return back the original button text**禁用状态,然后处理后,重新启用并返回原始按钮文本**

$(function () {

    $(".btn-Loading").each(function (idx, elm) {
        $(elm).click(function () {
            //do processing
            if ($(".input-validation-error").length > 0)
                return;
            $(this).attr("label", $(this).text()).text("loading ....");
            $(this).delay(1000).animate({ disabled: true }, 1000, function () {
                //original event call
                $.when($(elm).delay(1000).one("click")).done(function () {
                    $(this).animate({ disabled: false }, 1000, function () {
                        $(this).text($(this).attr("label"));
                    })
                });
                //processing finalized
            });
        });
    });
    // and fire it after definition
});

My solution:我的解决方案:

// jQuery plugin to prevent double submission of forms
$.fn.preventDoubleSubmission = function () {
    var $form = $(this);

    $form.find('[type="submit"]').click(function () {
        $(this).prop('disabled', true);
        $form.submit();
    });

    // Keep chainability
    return this;
};

In my case the form's onsubmit had some validation code, so I increment Nathan Long answer including an onsubmit checkpoint在我的情况下,表单的 onsubmit 有一些验证代码,所以我增加了Nathan Long答案,包括一个 onsubmit 检查点

$.fn.preventDoubleSubmission = function() {
      $(this).on('submit',function(e){
        var $form = $(this);
        //if the form has something in onsubmit
        var submitCode = $form.attr('onsubmit');
        if(submitCode != undefined && submitCode != ''){
            var submitFunction = new Function (submitCode);
            if(!submitFunction()){
                event.preventDefault();
                return false;
            }                   
        }

        if ($form.data('submitted') === true) {
            /*Previously submitted - don't submit again */
            e.preventDefault();
        } else {
          /*Mark it so that the next submit can be ignored*/
          $form.data('submitted', true);
        }
      });

      /*Keep chainability*/
      return this;
    };

Change submit button:更改提交按钮:

<input id="submitButtonId" type="submit" value="Delete" />

With normal button:带普通按钮:

<input id="submitButtonId" type="button" value="Delete" />

Then use click function:然后使用点击功能:

$("#submitButtonId").click(function () {
        $('#submitButtonId').prop('disabled', true);
        $('#myForm').submit();
    });

And remember re-enable button when is necesary:并记住在需要时重新启用按钮:

$('#submitButtonId').prop('disabled', false);

I can't believe the good old fashioned css trick of pointer-events: none hasn't been mentioned yet.我简直不敢相信指针事件的老式 css 技巧:还没有提到任何一个。 I had the same issue by adding a disabled attribute but this doesn't post back.我通过添加禁用属性遇到了同样的问题,但这不会回发。 Try the below and replace #SubmitButton with the ID of your submit button.尝试以下操作并将#SubmitButton 替换为您提交按钮的 ID。

$(document).on('click', '#SubmitButton', function () {
    $(this).css('pointer-events', 'none');
})

Why not just this -- this will submit the form but also disable the submitting button,为什么不只是这个——这将提交表单,但也会禁用提交按钮,

   $('#myForm').on('submit', function(e) {
       var clickedSubmit = $(this).find('input[type=submit]:focus');
       $(clickedSubmit).prop('disabled', true);
   });

Also, if you're using jQuery Validate, you can put these two lines under if ($('#myForm').valid()) .此外,如果您使用 jQuery Validate,您可以将这两行放在if ($('#myForm').valid())

I solved a very similar issue using:我使用以下方法解决了一个非常相似的问题:

$("#my_form").submit(function(){
    $('input[type=submit]').click(function(event){
        event.preventDefault();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM