0

I am having an issue getting my Ajax post to disable the button that I have to submit the data. Below is my code, I've looked around and everything looks right to me but its not disabling the button, I've tried to use $("#refreshButton").attr("disabled", true); and $("#refreshButton").removeAttr("disabled"); but it didn't work either.

$(document).ready(function () {
    $.ajax({
        type: "POST",
        async: false,
        beforeSend: function (request) {
            $("#refreshButton").attr("disabled", true); 
            request.setRequestHeader("AUTHORIZATION", authorizationToken);
        },
        url: url,
        dataType: "xml",
        success: function (xml) {
            //Do Something here
        });
        },
        error: function (x, status, error) {
            //errors                
        }
    });
})
$("#refreshButton").removeAttr("disabled");

This is my button:

<input id="refreshButton" type="button" onclick="RefreshDataSubmit();" value="Refresh"/>

Thanks for any help anyone can provide.

4

2 回答 2

4

一旦ajax调用完成,您必须启用按钮,并且您将在always处理程序中执行此操作,因为您不应该使用async false,并且无论ajax调用的结果如何,这将始终启用按钮(失败或完成)。
现在的问题是您在 DOM 就绪功能之外启用按钮,因此选择器没有找到该元素,或者它在 ajax 调用之前执行,因为文档就绪是异步的。

$(document).ready(function () {
    $.ajax({
        type: "POST",
        beforeSend: function (request) {
            $("#refreshButton").prop("disabled", true); 
            request.setRequestHeader("AUTHORIZATION", authorizationToken);
        },
        url: url,
        dataType: "xml"
    }).done(function (xml) {
         //Do Something here
    }).fail(function (x, status, error) {
         //errors                
    }).always(function() {
         $("#refreshButton").removeProp("disabled");
    });
});
于 2013-06-24T12:01:16.943 回答
1
$(document).ready(function () {
    $.ajax({
        type: "POST",
        beforeSend: function (request) {
            $("#refreshButton").prop("disabled", true);
            request.setRequestHeader("AUTHORIZATION", authorizationToken);
        },
        url: url,
        dataType: "xml",
        success: function (xml) {                
            //Do Something here
        },
        complete:function(){$("#refreshButton").prop("disabled", false);},
        error: function (x, status, error) {
          //errors                
        }...
于 2013-06-24T12:01:04.870 回答