ajax加载gif没有删除成功

时间:2016-04-28 04:02:22

标签: javascript php jquery ajax

我目前有一个表,通过ajax请求加载一堆结果,你可以看到下面的代码。但是,当我尝试添加一个加载gif时,我只看到页面上的加载gif,它在ajax请求是suscessfull之后不会自行删除

我可以看到该页面触发ajax请求并返回数据

我正在追逐加载栏,以便在ajax处理时显示,然后在加载时隐藏

这是html代码

<div class='content'>
    <div class='panel panel-flat' id='records_loading'>
        <table class='table table-togglable table-hover table-striped' data-limit-navigation='5' data-page-size='20'>
            <thead class='bg-teal'>
                <tr>
                    <th data-toggle='true' data-type='numeric'>SMS ID</th>
                    <th data-hide='phone'>Time Stamp</th>
                    <th data-hide='phone,tablet'>Mobile Number</th>
                    <th data-hide='phone,tablet'>Message</th>
                    <th data-hide='phone,tablet'>Message Status</th>
                </tr>
            </thead>
            <tbody  id='records_table'>
            </tbody>
        </table>
    </div>
<ul class='pagination pagination-separated hide-if-no-paging'></ul>
</div>

这是ajax代码

<script>
function showResult(str) {  
    $.ajax({
        url: '/getjson.php?search='+str, 
        type: 'POST',    
        dataType:'json',
        beforeSend:function(response)
       {
        $('#records_loading').html('<img src="assets/content/login.gif">');
       },
        success: function (response) 
        {
          var trHTML = '';
          $.each(response, function (key,value) {
             trHTML += 
                '<tr><td>' + value.ID + 
                '</td><td>' + value.Timestamp + 
                '</td><td>' + value.Number + 
                '</td><td>' + value.Message + 
                '</td><td>' + value.Status + 
                '</td></tr>';     
          });
            $('#records_table').html(trHTML);
            $('.table-togglable').trigger('footable_redraw');
        }
    }); 
}
showResult('');
</script>

4 个答案:

答案 0 :(得分:1)

    success: function (response) 
    {
       $('#records_loading').html('<table class='table table-togglable table-hover table-striped' data-limit-navigation='5' data-page-size='20'><thead class='bg-teal'>');
    }

答案 1 :(得分:1)

您可以尝试这样做以获得更好的用户体验和展示:

html://在html代码中的任意位置添加以下代码

<img src="assets/content/login.gif" id="loader" style="display:none;position:fixed;z-index:9999;top:50%;left:50%;">

的javascript:

<script>
function showResult(str) {  
    $("#loader").fadeIn();
    $.ajax({
    url: '/getjson.php?search='+str, 
    type: 'POST',    
    dataType:'json',
    success: function (response) 
    {
      var trHTML = '';
      $.each(response, function (key,value) {
         trHTML += 
        '<tr><td>' + value.ID + 
        '</td><td>' + value.Timestamp + 
        '</td><td>' + value.Number + 
        '</td><td>' + value.Message + 
        '</td><td>' + value.Status + 
        '</td></tr>';     
      });
        $('#records_table').html(trHTML);
        $('.table-togglable').trigger('footable_redraw');
    }
    });   
    $("#loader").fadeOut();
}
showResult('');
</script>

答案 2 :(得分:1)

只有一次更改..

使用

$('#records_table').html('<img src="assets/content/login.gif">');

而不是

$('#records_loading').html('<img src="assets/content/login.gif">');

最终守则:

<script>
function showResult(str) {  
    $.ajax({
        url: '/getjson.php?search='+str, 
        type: 'POST',    
        dataType:'json',
        beforeSend:function(response)
       {
        $('#records_table').html('<img src="assets/content/login.gif">');
        $('#records_loading thead.bg-teal').hide();
       },
        success: function (response) 
        {
          var trHTML = '';
          $.each(response, function (key,value) {
             trHTML += 
                '<tr><td>' + value.ID + 
                '</td><td>' + value.Timestamp + 
                '</td><td>' + value.Number + 
                '</td><td>' + value.Message + 
                '</td><td>' + value.Status + 
                '</td></tr>';     
          });
            $('#records_loading thead.bg-teal').show();
            $('#records_table').html(trHTML);
            $('.table-togglable').trigger('footable_redraw');
        }
    }); 
}
showResult('');
</script>

答案 3 :(得分:0)

我没有看到任何代码在成功时移除loading.gif。 想要显示您在beforeSend函数中编写代码段的gif,您可以编写代码以在成功时删除gif。以下代码段将清空record_loading容器

$('#records_loading').empty();