简体   繁体   English

延迟后提交表单数据,点击提交按钮?

[英]Submit Form-data after delay, on hitting a submit-button?

I am nearly finished my website, but my last problem is, I want to include a Loading.gif on the Follow-Button after hitting the submit-button 'Follow'. 我的网站即将完工,但是我的最后一个问题是,在单击“提交”按钮后,我想在“跟随”按钮上包含一个Loading.gif。 For this I need to submit the form-data after a delay. 为此,我需要在延迟后提交表单数据。 For showing the loading.gif on hitting submit, I dont need help, just for the delay. 为了在点击提交时显示loading.gif,我不需要帮助,只是为了延迟。

I tried different codes but they didnt work. 我尝试了不同的代码,但它们没有起作用。 Here are all the codes: 这是所有代码:

My Form-Tag with submit-button: 我的带有提交按钮的表单标签:

<form id=followForm action="" method="POST"><input type="submit" name="follow" class="btn_id9 shadow rounded-min ptr" value="Follow User" style="width:100%" /></form>

And here the code-sample, which didnt work: 这是无效的代码示例:

    function formdelay(followform) { 
      $(function() {
        setTimeout(function() { $('#followForm').submit(); }, 2000);
      });
    }

I hope you guys can help me, thanks! 我希望你们能帮助我,谢谢!

What you need to do is something like this: 您需要做的是这样的:

$("#followForm input").click(function(e) {
    e.preventDefault();
    setTimeout(function() { $("#followForm").submit(); }, 2000);
});

The key is e.preventDefault() . 密钥是e.preventDefault() This method will stop the default behavior of clicking on the submit button (which is submitting on the form). 此方法将停止单击“提交”按钮(在表单上提交)的默认行为。 You can then do whatever you need to do and then submit the form manually which is what $("#followForm").submit(); 然后,您可以执行所需的任何操作,然后手动提交表单,即$("#followForm").submit(); does. 做。

Demo: http://jsfiddle.net/DMcCr/2/ 演示: http//jsfiddle.net/DMcCr/2/

This is an interesting problem. 这是一个有趣的问题。 The first thought that I had was to do something like this: 我首先想到的是做这样的事情:

http://jsfiddle.net/V9UpA/ http://jsfiddle.net/V9UpA/

$('#followForm').on('submit', function (event, force) {
    if (!force) {
        var $this = $(this);
        event.preventDefault();
        setTimeout(function () {
            $this.trigger('submit', true);
        }, 2000);
    }
});

Basically, you want to use event.preventDefault() on the initial submit event, followed by a timeout. 基本上,您想在初始提交事件上使用event.preventDefault() ,然后设置超时。 After that timeout has completed, you re-trigger the event, but pass the force argument so that we want to allow the submit this time around. 超时完成后,您将重新触发该事件,但传递force参数,以便我们希望允许这次提交。

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

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