Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在等待post响应时添加正在加载的图像_Jquery - Fatal编程技术网

Jquery 在等待post响应时添加正在加载的图像

Jquery 在等待post响应时添加正在加载的图像,jquery,Jquery,我想在jquery中添加加载图像或更改鼠标光标,同时等待帖子的响应 jquery $.post(url,data, function(response){ //add a loading cursor to indicate that it is waiting for a response? alert(response); }); 我需要这个,因为响应需要很长时间才能弹出 有什么想法吗?谢谢在ajax调用之前更改光标,然后在成功处理程序中重新更改: $('body'

我想在jquery中添加加载图像或更改鼠标光标,同时等待帖子的响应

jquery

$.post(url,data,
    function(response){
    //add a loading cursor to indicate that it is waiting for a response?
    alert(response);
});
我需要这个,因为响应需要很长时间才能弹出


有什么想法吗?谢谢

在ajax调用之前更改光标,然后在成功处理程序中重新更改:

$('body').css('cursor','wait');

$.post(url,data, function(response){
    $('body').css('cursor','default');
});
如果使用$.ajax,您还有几个选项:

$.ajax({
    url : url,
    data: data,
    type: 'POST',
    beforeSend: function() {
        $('body').css('cursor','wait');
    }
}).done(function(data) {
    // do something with data
}).fail(function() {
    console.log('error');
}).always(function() {
    $('body').css('cursor','default');
});

同样的逻辑也适用于正在加载的图像或您决定添加的任何内容?

这里有一个使用$.ajax()的选项。您需要在DOM中定义隐藏的loadingImage

        $.ajax({                                                                                                           
            type:'POST',                                                                                                   
            ...                                                                                                           
            beforeSend:function(){                                                                                         
                $('.loadingImg').show();                                                                                
            },                                                                                                             
            complete:function(){                                                                                           
                $('.loadingImg').hide();                                                                    
            },                                                                                                             
            ...                                                                                                                                                         
        });