为什么不是't此图像显示在AJAX请求发送之前,并在完成时隐藏

Why isn't this image showing before the AJAX request is sent and hiding on completion?

本文关键字:隐藏 完成时 请求 AJAX 为什么不 显示 图像      更新时间:2023-09-26

在处理AJAX请求时,我试图使"加载"gif可见。所以我有

beforeSend: function () { 
   $('.form-sending-gif').show(); 
   console.log("The beforeSend function was called"); 
}

complete: function () { 
   $('.form-sending-gif').hide(); 
   console.log("The complete function was called"); 
}

其中CCD_ 1仅用于测试目的。由于未知的原因,我已经确认两个功能都在启动,但图像并没有像预期的那样显示隐藏。我在控制台上输入了$('.form-sending-gif').show(),以验证所述函数是否完成了预期的操作

我做错了什么?

如果你的问题没有更多细节,很难回答,但无论如何我都会接受。

你可以试试这样的东西。

$(function () {
    // On click func
    $('#test').click(function(){
        // Set loading image to display here
        $('#test').text('Greetings')
        // Simulated ajax request(put your ajax here)
        var ajaxCallDfd = $.Deferred();
        setTimeout(function () {
            ajaxCallDfd.resolve();
        }, 1000);
        // When the ajax is finished Then we display the loaded image.
        $.when(ajaxCallDfd).then(function(){
            $('#test').text('Goodbye')
        })
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="test"> Hi </div>