jQuery防止多个提交 [英] Jquery prevent multiple submit
问题描述
如果某人多次单击提交按钮之一,我想防止多次提交.
I want to prevent multiple submit if someone click on one of the submit buttons multiple times.
在这种情况下,unbind
或undelgate
怎么可能只调用一次我的自定义函数do_some_stuff
,因为我尝试了一些jquery方法,但是我认为做错了.谢谢
How can unbind
or undelgate
in this case the call of my custom function do_some_stuff
that is only happens one time, because I try some of the jquery methods, but I think I did something wrong. Thanks
$(function() {
$('div.ajax').delegate('form button', 'click', function(e) {
$(this).do_some_stuff();
e.preventDefault();
});
});
推荐答案
绑定和取消绑定在JQuery中已弃用.
Bind and unbind are deprecated in JQuery.
从jQuery 1.7开始,.on()
方法是将事件处理程序附加到文档的首选方法.
As of jQuery 1.7, the .on()
method is the preferred method for attaching event handlers to a document.
要回答有关多个提交的问题,JQuery 1.7中的另一个新增功能是.one()
处理程序,该处理程序将事件处理程序附加到对象,但只允许触发一次.这样可以防止多次提交.
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.
例如:
$("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.
这篇关于jQuery防止多个提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!