jQuery 如何延迟提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8828027/
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:06:36  来源:igfitidea点击:

How do I delay a form from submitting

jquery

提问by Chris

I have an additional function to execute before I submit the form. This isn't doing the trick.

在提交表单之前,我有一个额外的功能要执行。这不是诀窍。

$('form').submit( function(event) {
    var formId = $(this).attr('id');
    mySpecialFunction(formId);

    event.preventDefault();

    setTimeout( function () { 
        $(this).submit();
    }, 300);

}); 

This isn't working obviously.

这显然不起作用。

回答by lonesomeday

You need to fire the event on the form element itself, not on a jQuery selection. (In fact, you weren't even selecting the form element – inside setTimeout, thisis the global object.)

您需要在表单元素本身上触发事件,而不是在 jQuery 选择上。(事实上,你甚至没有选择表单元素-内setTimeoutthis是全局对象。)

Cache a reference to the form (this) and call its submitmethod:

缓存对表单 ( this)的引用并调用其submit方法:

$('form').submit( function(event) {
    var formId = this.id,
        form = this;
    mySpecialFunction(formId);

    event.preventDefault();

    setTimeout( function () { 
        form.submit();
    }, 300);
}); 

Note that I have also replaced your inefficient $(this).attr('id')call with this.id. Note also that you have to call the DOM form element's submit method, not the jQuery method, so that the jQuery event handler is not triggered, which would cause an infinite (and totally ineffectual) loop.

请注意,我还$(this).attr('id')this.id. 另请注意,您必须调用 DOM 表单元素的 submit 方法,而不是 jQuery 方法,以便不会触发 jQuery 事件处理程序,这会导致无限(且完全无效)循环。

回答by Esailija

$('form').submit( function(event) {
    var formId = this.id,
              $this = $(this);

    mySpecialFunction(formId);

    event.preventDefault();
    $this.unbind("submit");
    setTimeout( $.proxy( $.fn.submit, $this ), 300);

}); 

回答by Kevin B

Try to unbind the submit event, and within the setTimeout refer to the proper "this" instead of the window.

尝试解除提交事件的绑定,并在 setTimeout 中引用正确的“this”而不是窗口。

$(this).unbind("submit");
var self = this;
setTimeout( function () { 
  $(self).submit();
}, 300);

回答by Adaz

you can set a callback in your function

您可以在函数中设置回调

$('form').submit( function(event) {
    var formId = $(this).attr('id');

    mySpecialFunction(formId, $(this).doWhatever(formId );

    event.preventDefault();
});

function mySpecialFunction(id, onComplete) {

    if ($.isFunction(onComplete)) {
        onComplete.call(this);
    }
}

回答by bkunzi01

Also possible with the jquery delay method:

也可以使用 jquery 延迟方法:

$(function() {
    $('#the_form').delay(300).submit();
});

回答by Monojit Sarkar

another small code to delay form submit as follows

另一个延迟表单提交的小代码如下

$('form').submit(function (e) {
    var form = this;
    e.preventDefault();
    setTimeout(function () {
        form.submit();
    }, 1000); // in milliseconds

    $("<p>Delay...</p>").appendTo("body");
});

回答by Constantin Gro?

That's because the submit event is called over and over again, but is preventing the submission because of preventDefault. Either unbind the submit event after your function has been executed, or use a boolean value to set and check if it has been already executed.

那是因为提交事件被一遍又一遍地调用,但由于 preventDefault 阻止了提交。在您的函数执行后取消绑定提交事件,或者使用布尔值来设置并检查它是否已经执行。