在bootstrap模式框中加载远程数据之前添加等待消息

时间:2015-12-06 15:33:48

标签: php jquery css twitter-bootstrap-3 bootstrap-modal

我有这个代码用于将远程php数据加载到bootstrap模式框中:

$(function() {
    $(window).bind("resize", function() {
        if ($(this).width() < 400) {
            $('#tabs-content').removeClass('col-xs-10').addClass('col-xs-12')
        } else {
            $('#tabs-content').removeClass('col-xs-12').addClass('col-xs-10')
        }
    }).resize();
});
$(function() {  
    $(document).on('click', '.push', function(e) {
        e.preventDefault();
        var id = $(this).attr('id'); 
        $.ajax({
            type: 'post',
            url: 'details.php', // in here you should put your query 
            data: {
                'bookid': id,
                'lang': 'fa',
                'csrf_token': 'MTQ0OTQxNDQ0MUVvN2JwNXNJRHVxMDZmOXFpQm1ROFNNTk1UZ3lPMGZO'
            }, 
            success: function(r) {
                // now you can show output in your modal 
                $('#bookdetails').modal({
                        backdrop: 'static',
                        keyboard: false
                    }) // put your modal id 
                $('.something').show().html(r);
            }
        }); 
    });
});

这对我有用但我需要在加载数据之前显示加载message/image

如何添加等待消息/图标?!

1 个答案:

答案 0 :(得分:2)

您只需要在image/message之前显示Ajax call并将其隐藏在success: function(r)

假设您有模态加载前显示的图像,图像HTML例如

<img class="progress" src="http://downgraf.com/wp-content/uploads/2014/09/01-progress.gif" style="display:none;">

在JS中,只需使用.show()函数显示图片,并在success: function(r)隐藏模态加载后使用.hide()函数

$(document).on('click', '.push', function(e) {
    e.preventDefault();
    var id = $(this).attr('id');
    $(".progress").show(); // <-- Show progress image
    $.ajax({
        type: 'post',
        url: 'details.php', // in here you should put your query 
        data: {
            'bookid': id,
            'lang': 'fa',
            'csrf_token': 'MTQ0OTQxNDQ0MUVvN2JwNXNJRHVxMDZmOXFpQm1ROFNNTk1UZ3lPMGZO'
        }, 
        success: function(r) {
            // now you can show output in your modal 
            $('#bookdetails').modal({
                    backdrop: 'static',
                    keyboard: false
                }) // put your modal id 
            $('.something').show().html(r);
            $(".progress").hide(); // <-- Hide progress image
        }
    });
});

Minimal example with delay and fade