想要显示ajax加载gif

时间:2011-04-11 06:41:59

标签: php jquery ajax

我正在使用DataTables。我的所有代码都运行正常。现在我想把ajax加载gif。任何人都可以帮我把ajax加载器gif?这是我的代码。感谢

         <script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
        $("#dvloader").show();
            oTable = $('#example').dataTable({
                "bJQueryUI": true,
                "sPaginationType": "full_numbers"                   
            });
        } );                 
         (document).ready(function() {
            $("#btnround").click(function(){
                $.ajax({
                  url: "ajax_request.php",
                  cache: false,
                  async: true,
                  data: "shape=ROUND",
                  success: function(html){
                    $(".demo_jui").html(html);
                }
                });
            });
    });
           </script>

3 个答案:

答案 0 :(得分:6)

使用

  

ajaxStart()

  

ajaxComplete()

显示和隐藏加载gif的函数。

$("#loading").ajaxStart(function(){
   $(this).show();
 });

$("#loading").ajaxComplete(function(){
   $(this).hide();
 });

具有id

的div或元素
  

装载

有加载gif。

您的最终代码应如下所示:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $("#dvloader").show();
        oTable = $('#example').dataTable({
            "bJQueryUI": true,
            "sPaginationType": "full_numbers"
        });
    });
    (document).ready(function() {
        $("#btnround").click(function() {
            $.ajax({
                url: "ajax_request.php",
                cache: false,
                async: true,
                data: "shape=ROUND",
                success: function(html) {
                    $(".demo_jui").html(html);
                }
            });
        });
        $("#loading").ajaxStart(function(){
          $(this).show();
        });

        $("#loading").ajaxComplete(function(){
          $(this).hide();
        });        

    });
</script>

答案 1 :(得分:3)

希望这会对你有所帮助。添加beforeSend和complete属性并调用各自的函数。

$("#btnround").click(function(){
     $.ajax({
     url: "ajax_request.php",
     cache: false,
     async: true,
     data: "shape=ROUND",
     beforeSend : fnLoadStart,
     complete : fnLoadStop,
     success: function(html){
         $(".demo_jui").html(html);
         }                 
         });             
     }); 

     function fnLoadStart() {
           $("#dvloader").show();  // this div should have loader gif
     }
     function fnLoadStop() {
          $("#dvloader").hide();  
     }

答案 2 :(得分:0)

这应该很简单:

<script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
        $("#dvloader").show();
            oTable = $('#example').dataTable({
                "bJQueryUI": true,
                "sPaginationType": "full_numbers"                   
            });
        } );                 
         (document).ready(function() {
            $("#btnround").click(function(){
                $(".demo_jui").html('<img src="path/to/loading.gif" />');
                $.ajax({
                  url: "ajax_request.php",
                  cache: false,
                  async: true,
                  data: "shape=ROUND",
                  success: function(html){
                    $(".demo_jui").html(html);
                }
                });
            });
    });
           </script>
相关问题