Change submit button to a loading image with jquery

后端 未结 9 1106
庸人自扰
庸人自扰 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('<img src="/loading.gif" />');
        });
    });
    

    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('<img src="/loading.gif" />');  
            // don't let this form be submitted again
            $(this).submit(function() { return false; });
        });
    });
    
    0 讨论(0)
  • 2021-02-06 11:58

    Event can be binded on form as a standard practice

    <script type="text/javascript">
         $('form').submit(function(){
             $('#btnSubmit').attr('disabled','disabled');
             $('#divMsg').show();
             //your client side validation here
             if(valid)
                return true;
             else
                {
                  $(this).removeAttr('disabled');
                  $('#divMsg').hide();     
                  return false;
                }
         });
    </script>
    
    0 讨论(0)
  • 2021-02-06 12:01

    I think you are looking for this one ,button have this function updatemycartpage() in onclick event

    <script language="JavaScript">
         function updatemycartpage(index,itemId,itemnumber){
    
       jQuery('#Page').html('<p><center><img src="/images/ajax-loader.gif"/></center></p>');
        $.post("/cart.php", {
            'hidItemId[]' : itemId,
            'hidLineId[]' : itemnumber
    
        },function(data){
            document.location.href ="/cart.php";
        });
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题