【发布时间】:2011-01-18 10:08:25
【问题描述】:
从 previous question 开始,我询问是否禁用提交按钮,直到所有 ajax 调用都完成返回...
即使按钮被禁用并带有警告标志,人们似乎仍在设法提交表单。我想这可能是在文本输入中按“输入”。
如何禁用整个表单,而不仅仅是提交按钮?
目前的代码是:
// if we're waiting for any ajax to complete we need to disable the submit
$(document).ajaxStart(function() {
$(".ajaxbutton").attr("disabled", "disabled");
// if it's taken longer than 250ms display waiting message
timer = setTimeout(function() {
$('#processingAlert').html(' ...please wait one moment');
$('#processingAlert').show();
}, 250);
})
$(document).ajaxComplete(function() {
$(".ajaxbutton").removeAttr("disabled");
$('#processingAlert').hide();
// cancel showing the message when the ajax call completes.
clearTimeout(timer);
});
我应该提到的另一件事可能导致问题是同时发生多个 ajax 调用,例如,一个 div 接收隐藏的输入,而页面上其他地方的另一个 div 显示总价格。
一些 ajax 调用正在快速完成这一事实是否会抵消 ajaxStart 的禁用效果? EG ajax call1 和 ajax call2 都触发 ajaxStart - call1 完成得非常快,它会在我们等待 call2 完成时重新启用表单吗?
有没有更好的方法来测试是否所有 ajax 调用都已完成?
【问题讨论】:
标签: jquery ajax validation forms