我在获取Ajax帖子时遇到问题,要禁用我必须提交数据的按钮。下面是我的代码,我环顾四周,一切看起来都对我,但它没有禁用按钮,我试图使用$("#refreshButton").attr("disabled", true);
和$("#refreshButton").removeAttr("disabled");
,但它也没有用。
$(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");
这是我的按钮:
<input id="refreshButton" type="button" onclick="RefreshDataSubmit();" value="Refresh"/>
感谢任何人提供的任何帮助。
答案 0 :(得分: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");
});
});
答案 1 :(得分: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
}...