简体   繁体   中英

jQuery: delay disable button onclick after the form submission?

So right now I have this:

HTML:

<input type="submit" class="usp-submit usp-submit-default"  onclick="return change(this); "  id="btnSubmit" />

jQuery:

function change( el )

{
    if ( el.value === "Send" )
        el.value = "Sending";
    else
        el.value = "Please wait...";

$("#btnSubmit").attr("disabled", true);
    
}

This does disable the input button and change the value to "Please wait", but the form is not submitted.

I assume this disables the button in the first place and doesn't perform the send action.

I have read somewhere that you can add a delay before .attr("disabled", true) for a few milliseconds so that the form would be submitted first.

Any help from jQuery experts would be appreciated.

Thanks.

If you want to delay, you can use the vanilla js way, setTimeout()

function change( el )
{
    if ( el.value === "Send" )
        el.value = "Sending";
    else
        el.value = "Please wait...";

    setTimeout( () => $("#btnSubmit").prop("disabled", true) , 0);
}

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