Show “loading” animation on button click

前端 未结 6 924
感情败类
感情败类 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):

      
      
    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.

提交回复
热议问题