1

I have the following script which requests data through AJAX:

 $(document).ready(function(){
        $('#input').keyup(function(){
            sendValue($(this).val());     
        }); 

    });
    function sendValue(str){
        $.post("ajax.php",{ sendValue: str },
        function(data){
            $('#display').html(data.returnValue);
        }, "json");

    }

I just want to show a DIV while the request is being made, and hide it when the data comes back. I've tried this:

$("#loading").ajaxStart(function(){
   $(this).show();
})
.ajaxStop(function(){
   $(this).hide();
});

But it didn't show or hide, it was always displayed.

4

1 回答 1

3

工作 jsFiddle 演示

jQuery 1.8开始,该.ajaxStart()方法只能附加到document. 所以:

$(document).ajaxStart(function(){
    $("#loading").show();
})
.ajaxStop(function(){
    $("#loading").hide();
});


参考:

于 2013-06-08T03:56:19.717 回答