运行POST时显示图像

Display image while running POST

本文关键字:显示图 图像 显示 POST 运行      更新时间:2023-09-26

我有这样的代码,可以在不重新加载页面的情况下进行POST,但我想知道如何将其添加到正在执行的显示图像的脚本中。示例loader.gif

谢谢你的帮助。

<script language="javascript" src="jquery-1.6.4.min.js"></script>
<script language="javascript">
$(document).ready(function() {
    $().ajaxStart(function() {
        $('#loading').show();
        $('#result').hide();
    }).ajaxStop(function() {
        $('#loading').hide();
        $('#result').fadeIn('slow');
    });
    $('#form, #fat, #form').submit(function() {
        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(data) {
                $('#result').html(data);
            }
        })
        return false;
    }); 
})  

不确定我是否理解,但你似乎很接近

// #loading could be an img tag pointing to loader.gif or a div containing the img
$('#loading').ajaxStart(function() {
    $(this).show();
    $('#result').hide();
}).ajaxStop(function() {
    $(this).hide();
    $('#result').fadeIn('slow');
});

使用JQuery将图像添加到页面中。通常,这是通过用户单击按钮来启动AJAX请求来添加的。这允许用户在脚本运行时看到后台发生了什么。

或者,由于您的脚本是在document.ready上运行的,您可以从一开始就获得图像,然后在ajax调用完成后将其删除。

将加载程序放在一个隐藏的div(display:none)中,然后可以使用

$('#div').show();

在ajax请求之前,然后在成功回调中再次隐藏它:

$('#div').hide();

submit函数的第一行:

$('#loadingImg').show();

在回调CCD_ 2函数的第一行:

$('#loadingImg').hide();