jQuery jquery防止多次提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9719376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:11:28  来源:igfitidea点击:

Jquery prevent multiple submit

jquery

提问by Azd325

I want to prevent multiple submit if someone click on one of the submit buttons multiple times.

如果有人多次单击提交按钮之一,我想防止多次提交。

How can unbindor undelgatein this case the call of my custom function do_some_stuffthat is only happens one time, because I try some of the jquery methods, but I think I did something wrong. Thanks

如何unbind或者undelgate在这种情况下调用我的自定义函数do_some_stuff只发生一次,因为我尝试了一些 jquery 方法,但我认为我做错了什么。谢谢

$(function() {
    $('div.ajax').delegate('form button', 'click', function(e) {
        $(this).do_some_stuff();
        e.preventDefault();
    });
});

回答by reach4thelasers

Bind and unbind are deprecated in JQuery.

JQuery 中不推荐使用绑定和解除绑定。

As of jQuery 1.7, the .on()method is the preferred method for attaching event handlers to a document.

从 jQuery 1.7 开始,该.on()方法是将事件处理程序附加到文档的首选方法。

http://api.jquery.com/on/

http://api.jquery.com/on/

To answer your question about multiple submits, another new addition in JQuery 1.7 is the .one()handler which, attaches an event handler to an object but only allows it to be fired once. This will allow you to prevent multiple submits.

为了回答您关于多次提交的问题,JQuery 1.7 中的另一个新增功能是.one()处理程序,它将事件处理程序附加到一个对象,但只允许它被触发一次。这将允许您防止多次提交。

e.g:

例如:

$("form#form1").one("submit", submitFormFunction);

function submitFormFunction(event) {
    event.preventDefault(); 
    $("form#form1").submit();
}

Note I'm binding to the form submit event rather than a button click event.

注意我绑定到表单提交事件而不是按钮单击事件。

回答by ncremins

There are two ways of doing this as pointed out by cHao.

正如 cHao 所指出的,有两种方法可以做到这一点。

$('form button').prop('disabled', true);

or

或者

$('form button').attr('disabled', 'disabled');

回答by Simon

when you click the button within the function add the attr(disabled) which will make the button inactive.

当您单击函数中的按钮时,请添加 attr(disabled),这将使按钮处于非活动状态。

$("form Button").attr("disabled","1"); 

should prevent multiple submissions

应该防止多次提交

回答by mukundi

If you have users who insist on having an active submit button all the time you can use a variable to track the progress. Something like this:

如果您的用户一直坚持使用活动提交按钮,您可以使用变量来跟踪进度。像这样的东西:

saving_record = 0; //set variable to show if form is in submission
$(document).ready(function () {
    $('#form').on('submit', function (e) {
        if (saving_record === 0) { //If not submitted yet, submit form
            saving_record = 1; //Set variable to show form is in submission
            $.ajax({
                success: function (data) {
                    saving_record = 0; //Reset variable to allow record update
                },
                error: function (data) {
                    saving_record = 0; //Reset variable to allow record update in case of error
                }
            })
        }
    });
});

回答by Abhishek Chandel

If you are using ASP.NET MVC Data Annotations for Client side Validation, you have to validate the form first -

如果您使用 ASP.NET MVC 数据注释进行客户端验证,则必须先验证表单 -

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

回答by JDE10

This works on submit

这适用于提交

$("form").submit(function() {
    // submit more than once return false
    $(this).submit(function() {
        return false;
    });
    // submit once return true
    return true;
});

回答by Kimon Moutsakis

$(document).ready(function(){
  $('div.ajax form').submit(function(){
     $(this).do_some_stuff();
     return false;
   });          
});