Change submit button to a loading image with jquery

后端 未结 9 1304
庸人自扰
庸人自扰 2021-02-06 11:12

On a lot of sites lately, I\'ve seen buttons being replaced with loading/thinking images after they are pressed, to prevent double clicks and make sure the user knows something

9条回答
  •  眼角桃花
    2021-02-06 11:58

    This replaces all submit inputs with an image onsubmit of the form

    $(function() {
        $("form").submit(function() {
            $(":submit").replaceWith('');
        });
    });
    

    You may also want to disable submitting the form again, in case the user hits enter in a form field...

    $(function() {
        // only run this handler on the first submit of the form 
        $("form").one("submit", function() {
            // replace all submit inputs with an image
            $(":submit").replaceWith('');  
            // don't let this form be submitted again
            $(this).submit(function() { return false; });
        });
    });
    

提交回复
热议问题