Javascript 全球ajaxStop和form';s自导函数
我有一个显示加载微调器的代码:Javascript 全球ajaxStop和form';s自导函数,javascript,ajax,jquery,Javascript,Ajax,Jquery,我有一个显示加载微调器的代码: $(document) .ajaxStart(function () { var indicator = $('#busyIndicator'); indicator.show(); }) .ajaxStop(function () { var indicator = $('#busyIndicator'); indicator.hide(); }) 我有一个关于表单成功的函数:
$(document)
.ajaxStart(function () {
var indicator = $('#busyIndicator');
indicator.show();
})
.ajaxStop(function () {
var indicator = $('#busyIndicator');
indicator.hide();
})
我有一个关于表单成功的函数:
function onSuccess() {
$('#busyIndicator').show();
}
这是html(省略了一些代码和属性):
在这种情况下,图像是可见的。我认为问题在于
ajaxStop
在onSuccess
之后触发。我只能在onSuccess
功能中更改代码。如何解决此问题?如果希望ajax调用不使用全局ajaxStart和ajaxStop。您需要在ajax调用中将其指定为一个选项。只需将global设置为false
$.ajax({
url: '/path/to/file',
type: 'default GET (Other values: POST)',
dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {param1: 'value1'},
global: false // this disables the global ajaxStart and ajaxStop events.
})
.done(function(response) {
console.log("success");
//do something on success
})
.fail(function(error) {
console.log("error");
//do something on error
})
.always(function() {
console.log("complete");
//this will be done always unrelated to succes or error
});
还可以在需要时使用名称空间绑定和解除绑定ajaxStart和AjaxStop事件。这里已经解释了这一点:
在AJAX上启动您。在AJAX上显示指示灯,在AJAX上停止隐藏指示灯,但为什么要在成功时显示指示灯?我不明白,我想在成功的时候你会想隐藏它,没有你的onsuccess函数,代码不是可以工作吗?你需要在你的ajax调用中添加一个选项:“global:true”@johnSmith:
ajaxStop
和ajaxStart
我在所有表单中全局使用。但是对于这个表单,我需要这个例子,因为ajax请求后会有重定向(这需要很长时间),我想再次显示它(当浏览器处于思考状态时),全局ajax事件会冒泡到文档中,因此它们会在$中设置的选项后触发。ajax
,而asajaxStop
基于complete
处理程序,如果在success
as之后触发well@user348173然后您需要添加选项“global:false”,那么ajaxStart和stop将不会发生
$('#busyIndicator').show();
$.ajax({
url: '/path/to/file',
type: 'default GET (Other values: POST)',
dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {param1: 'value1'},
global: false // this disables the global ajaxStart and ajaxStop events.
})
.done(function(response) {
console.log("success");
//do something on success
})
.fail(function(error) {
console.log("error");
//do something on error
})
.always(function() {
console.log("complete");
//this will be done always unrelated to succes or error
});