最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Submitting form after using e.preventDefault(); - Stack Overflow

programmeradmin2浏览0评论

I have a form that I am stopping from submitting with e.preventDefault(). (I've also tried return false).

I would manually tell the form to submit after a short delay, using the code:

$('form').delay(2000).submit();

Unfortunately, e.preventDefault() seems to disable the form from submitting even if it explicitly submitted using the submit() function.

Any idea how I can bine these intentions? I'd like to show a loading screen for a couple of seconds.

Thanks!

I have a form that I am stopping from submitting with e.preventDefault(). (I've also tried return false).

I would manually tell the form to submit after a short delay, using the code:

$('form').delay(2000).submit();

Unfortunately, e.preventDefault() seems to disable the form from submitting even if it explicitly submitted using the submit() function.

Any idea how I can bine these intentions? I'd like to show a loading screen for a couple of seconds.

Thanks!

Share Improve this question asked Feb 14, 2013 at 1:59 Chris NoletChris Nolet 9,0838 gold badges69 silver badges95 bronze badges 1
  • 2 I think delay() is only used for effects, not events. – sevaor Commented Feb 14, 2013 at 2:51
Add a ment  | 

4 Answers 4

Reset to default 2
$("form").on('submit.blocker', function (e) {
    setTimeout(function () {
        $("form").off('submit.blocker').trigger('submit');
    }, 2000);
    e.preventDefault();
});

Try this.

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function ()
    {
        var okToSubmit = false;
        $('form').submit(function (e)
        {
            if ( ! okToSubmit)
            {
                e.preventDefault();

                var $form = $(this);
                setTimeout(function ()
                {
                    okToSubmit = true;
                    $form.submit();
                }, 2000);
            }
        });
    });
</script>
<form action="somewhere">
    <input type="text" name="something" />
    <input type="submit" />
</form>

Above code is tested.

$('form').on('submit', function(e) {
    if (!e.isTrigger) {
        setTimeout(function() {
            $("form").trigger('submit');
        }, 2000);
        e.preventDefault();
    }
});

you may want to try this it works for me:

<form name="demo" action="111" onSubmit="fun();return false">
<input type="text" name="name1" />
<input type="submit" />
</form>

and in the javascript

fun()
{
var x= document.forms["demo"];
setTimeout(x.submit(),2000);//time in milliseconds
}
发布评论

评论列表(0)

  1. 暂无评论