Show “loading” animation on button click

前端 未结 6 940
感情败类
感情败类 2021-01-30 14:10

I want to do something very simple. I have one button in my page.

相关标签:
6条回答
  • 2021-01-30 14:27

    If you are using ajax then (making it as simple as possible)

    1. Add your loading gif image to html and make it hidden (using style in html itself now, you can add it to separate CSS):

      <img src="path\to\loading\gif" id="img" style="display:none"/ >
      
    2. Show the image when button is clicked and hide it again on success function

      $('#buttonID').click(function(){
        $('#img').show(); //<----here
        $.ajax({
          ....
         success:function(result){
             $('#img').hide();  //<--- hide again
         }
      }
      

    Make sure you hide the image on ajax error callbacks too to make sure the gif hides even if the ajax fails.

    0 讨论(0)
  • 2021-01-30 14:29
    $("#btnId").click(function(e){
          e.preventDefault();
          $.ajax({
            ...
            beforeSend : function(xhr, opts){
                //show loading gif
            },
            success: function(){
    
            },
            complete : function() {
               //remove loading gif
            }
        });
    });
    
    0 讨论(0)
  • 2021-01-30 14:32

    The best loading and blocking that particular div for ajax call until it succeeded is Blockui

    go through this link http://www.malsup.com/jquery/block/#element

    example usage:

     <span class="no-display smallLoader"><img src="/images/loader-small.png" /></span>
    

    script

    jQuery.ajax(
    {   
     url: site_path+"/restaurantlist/addtocart",
     type: "POST",
     success: function (data) {
       jQuery("#id").unblock(); 
    },
    beforeSend:function (data){
        jQuery("#id").block({ 
        message: jQuery(".smallLoader").html(), 
        css: { 
             border: 'none', 
             backgroundColor: 'none'
        },
        overlayCSS: { backgroundColor: '#afafaf' } 
        });
    
    }
    });
    

    hope this helps really it is very interactive.

    0 讨论(0)
  • 2021-01-30 14:35
                //do processing
                $(this).attr("label", $(this).text()).text("loading ....").animate({ disabled: true }, 1000, function () {
                    //original event call
                    $.when($(elm).delay(1000).one("click")).done(function () {//processing finalized
                        $(this).text($(this).attr("label")).animate({ disabled: false }, 1000, function () {
                        })
                    });
                });
    
    0 讨论(0)
  • 2021-01-30 14:37

    I know this a very much late reply but I saw this query recently And found a working scenario,

    OnClick of Submit use the below code:

     $('#Submit').click(function ()
     { $(this).html('<img src="icon-loading.gif" />'); // A GIF Image of Your choice
     return false });
    

    To Stop the Gif use the below code:

    $('#Submit').ajaxStop();

    0 讨论(0)
  • 2021-01-30 14:41

    try

    $("#btnId").click(function(e){
     e.preventDefault();
     //show loading gif
    
     $.ajax({
      ...
      success:function(data){
       //remove gif
      },
      error:function(){//remove gif}
    
     });
    });
    

    EDIT: after reading the comments

    in case you decide against ajax

    $("#btnId").click(function(e){
         e.preventDefault();
         //show loading gif
         $(this).closest('form').submit();
    });
    
    0 讨论(0)
提交回复
热议问题