简体   繁体   English

防止在 jQuery 效果运行时提交

[英]Prevent submit while jQuery effect is running

I have this issue that when user clicks a button (in this case, a submit button) multiple times, jQuery will keep playing the animation effects until it has completed the count of clicks the user has imputed.我有这个问题,当用户多次单击按钮(在本例中为提交按钮)时,jQuery 将继续播放 animation 效果,直到完成用户估算的点击次数。

This can get quite overwhelming.这可能会让人不知所措。

How can jQuery tell if it's currently executing an animation to a particular element, so I can prevent user from submitting while the elements effect is still in play? jQuery 如何判断它当前是否正在对特定元素执行 animation,因此我可以阻止用户在元素效果仍在播放时提交?

Notes: the submit button is in a file.注意:提交按钮在文件中。 Form handling is relayed via AJAX this jQuery is inside the ajax called file.表单处理通过 AJAX 中继,此 jQuery 在 ajax 称为文件内。

Here is the main files code:以下是主要文件代码:

$('#login_form').submit(function(e) {
        $.post(
               "ajax.php",
               { user: $('[name="username"]').val(), pw: $('[name="password"]').val() },
               function(resposeText) { $('#login_form_response').html(resposeText); },
               "html"
               );
        e.preventDefault();
    });

Here is the code (in ajax'ed' file):这是代码(在 ajax'ed' 文件中):

$('#login_form_response').html('Username or Password is inaccurate!')
.slideDown()
.delay(3500)
.slideUp(1500);

You could unbind the event-handler just before starting the animation, and in the callback function of the animation, just bind the handler again.您可以在启动 animation 之前取消绑定事件处理程序,并在 animation 的回调 function 中再次绑定处理程序。

$('#button').unbind('click');

$('#animated_element').animate({ animation, stuff}, 1000, function(){
   $('#button').bind('click', handlerFunc);
});

Note:笔记:

This is a way to prevent submitting when you are using a customized button (div, or a link), which has an event handler binded to it.当您使用绑定了事件处理程序的自定义按钮(div 或链接)时,这是一种防止提交的方法。 It does not work on pure html <input type="submit" /> - buttons, because after unbinding, the standard-submit is going to take effect.它不适用于纯 html <input type="submit" /> - 按钮,因为取消绑定后,标准提交将生效。 I prefer to use customized buttons, mainly because of styling (especially for IE7 and such).我更喜欢使用自定义按钮,主要是因为样式(尤其是 IE7 等)。

If you want to use a pure html-submit button, you'll have to disable the button (and disabling submit over "enter") or set a flag, that prevents submitting, as other users have already stated in their answers!如果您想使用纯 html-submit 按钮,则必须禁用该按钮(并禁用通过“enter”提交)或设置一个标志,以防止提交,正如其他用户在他们的答案中已经说明的那样!

Disable the submit button until the animation finishes.禁用提交按钮,直到 animation 完成。

$('animatingElementSelector').animate({height: 200px;}, slow, function() { //ENABLE BUTTON HERE });
var isAnimationRunning;


jQuery('#myForm').bind('submit',function(e){

   if(isAnimationRunning)
    {
        e.preventDefault();
    }
});

Create a variable isAnimationRunning let it know, animation running or not.创建一个变量isAnimationRunning让它知道,animation 运行与否。 If running then ignore submit.如果运行则忽略提交。

http://jsfiddle.net/praveen_prasad/cfvRf/ http://jsfiddle.net/praveen_prasad/cfvRf/

On demo click start animation and then try to submit!!在演示上单击开始 animation 然后尝试提交!

Edit 1编辑 1

Some ppl have suggested unbinding click of submit button, that wont work.一些人建议取消绑定单击提交按钮,这不起作用。 If a submit button is inside a form, clicking it will submit the form, you dont bind click event to submit form, its just pure html, so unbinding wont help in stopping submit event.如果提交按钮在表单内,单击它将提交表单,您不会将单击事件绑定到提交表单,它只是纯 html,因此取消绑定不会帮助停止提交事件。 check this demo检查这个演示

Edit 2 Some ppl have suggested disable the submit button during the animation.编辑 2一些人建议在 animation 期间禁用提交按钮。 That wont always work either, consider a situation where user types something in text box and press enter key, form will be submitted(some browsers will) regardless of submit button being disabled.这也不会总是有效,考虑用户在文本框中输入内容并按下回车键的情况,无论提交按钮被禁用,表单都将被提交(某些浏览器会)。

Just add disabling button when it clicked:) or hide... or both of this... hide and disable只需在单击时添加禁用按钮:)或隐藏...或两者兼而有之...隐藏和禁用

Try this:尝试这个:

$('#login_form').submit(function(e) {
        $('input[type=submit]', this).attr('disabled', 'disabled').attr('style','opacity: 0.5; filter: alpha(opacity = 50); ');
        $.post(
               "ajax.php",
               { user: $('[name="username"]').val(), pw: $('[name="password"]').val() },
               function(resposeText) { $('#login_form_response').html(resposeText); },
               "html"
               );
        e.preventDefault();
    });

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

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