简体   繁体   中英

Submitting form after using e.preventDefault();

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 combine these intentions? I'd like to show a loading screen for a couple of seconds.

Thanks!

$("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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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