jQuery隐藏和显示函数不能与AJAX调用一起使用

时间:2013-04-22 19:58:03

标签: jquery html ajax jquery-ui

想要在ajax调用处理时显示加载程序。但隐藏和显示在运行时不起作用。在调试模式下没关系。我试图在ajax上设置超时,但没有结果。

function Rate() {
    $("#rate_navigation").hide();
    $("#rate_loader").show();

    $.ajax({
        type: "POST",
        url: "url",
        data: "data",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (msg) {
            $("#comment_result").html(msg.d);
        },
        error: AjaxCallError
    });

    $("#rate_loader").hide();
    $("#rate_navigation").show();
}

3 个答案:

答案 0 :(得分:4)

beforeSendcomplete

请参阅http://api.jquery.com/jQuery.ajax/

function Rate() {

    $.ajax({
        type: "POST",
        url: "url",
        data: "data",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        beforeSend: function () {
            $("#rate_navigation").hide();
            $("#rate_loader").show();
        },
        complete: function () {
            $("#rate_loader").hide();
            $("#rate_navigation").show();
        },
        success: function (msg) {
            $("#comment_result").html(msg.d);
        },
        error: AjaxCallError
    });
}

答案 1 :(得分:1)

将这些投入到成功回调中:

success: function (msg) {
        $("#rate_loader").hide();
        $("#rate_navigation").show();
        $("#comment_result").html(msg.d);
    },
    error: AjaxCallError
});

答案 2 :(得分:1)

将取消隐藏条件置于成功之中

function Rate() {
$("#rate_navigation").hide();
$("#rate_loader").show();

$.ajax({
    type: "POST",
    url: "url",
    data: "data",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    success: function (msg) {
        $("#comment_result").html(msg.d);


        $("#rate_loader").hide();
        $("#rate_navigation").show();
    },
    error: AjaxCallError
});


}