jQuery ajaxStart和ajaxStop无法重新绑定
我正在为两个不同的事件绑定不同的加载消息 下面是我用来在AJAX调用之前绑定行为的函数。我在每次提交匹配的Ajax之前调用每个函数,以便为每个函数显示正确的加载消息:jQuery ajaxStart和ajaxStop无法重新绑定,jquery,bind,Jquery,Bind,我正在为两个不同的事件绑定不同的加载消息 下面是我用来在AJAX调用之前绑定行为的函数。我在每次提交匹配的Ajax之前调用每个函数,以便为每个函数显示正确的加载消息: function BindPersonalityLoader() { // Bind the loader to personality $('document').bind("ajaxStart", function() { // Show the loader $('img#p
function BindPersonalityLoader()
{
// Bind the loader to personality
$('document').bind("ajaxStart", function() {
// Show the loader
$('img#personality_loader').show();
});
// Bind the loader to personality
$('document').bind("ajaxStop", function() {
// Show the loader
$('img#personality_loader').hide();
});
}
function BindLoadEditPersonalityLoader()
{
// Bind the loader for editing a personality
$('document').bind("ajaxStart", function() {
// Hide the edit form
$('div#quiz_maker_add_personality_wrapper').hide();
// Show the loader
$('div#quiz_maker_edit_personality_loader').show();
});
// Bind the loader for editing a personality
$('document').bind("ajaxStop", function() {
// Hide the loader
$('div#quiz_maker_edit_personality_loader').hide();
});
}
一旦我调用了BindLoadEditPersonalityLoader()
,出于某种原因,这将成为永久性的,即使我随后在代码中调用了BindPersonalityLoader()
,我也会期待其他行为
出于我调用代码的某些原因,它没有覆盖“bind”
例如,如果我运行以下代码,BindLoadEditPersonalityLoader()
的行为始终保持不变,因为它是首先调用的:
// Bind the loaders once
BindLoadEditPersonalityLoader();
// Bind the loaders to different behaviour
BindPersonalityLoader();
我已经包含了两条alert()
消息,以便我可以看到它们何时被调用
我试过调用unbind(“ajaxStart ajaxStop”)代码>但这也不起作用
这就是我在两个不同事件中调用绑定的方式:
// Add a new personality to the quiz
$('form#personality_editor').submit(function(e) {
e.preventDefault();
// Bind the loader to personality loader
BindPersonalityLoader();
// AJAX Call here
});
// Edit a personality already existing within the quiz
$('a.personality_edit').live('click', function(e) {
e.preventDefault();
// Bind loader for editing personality
BindLoadEditPersonalityLoader();
// Ajax call here
});
绑定没有被“覆盖”,因为您正在将事件绑定到不同的对象
$('img#personality_loader').bind("ajaxStart", function() {...});
$('div#quiz_maker_edit_personality_loader').bind("ajaxStart", function() {...});
尝试绑定/解除绑定$(文档)
(不带的)绑定没有被“覆盖”,因为您正在将事件绑定到不同的对象
$('img#personality_loader').bind("ajaxStart", function() {...});
$('div#quiz_maker_edit_personality_loader').bind("ajaxStart", function() {...});
尝试绑定/解除绑定$(文档)
(不带的)ajaxStop
和ajaxStart
都是全局事件,在没有其他挂起的ajax请求时,只要启动ajax请求,就会在所有元素上触发该事件,每次ajax请求结束时,没有其他挂起的ajax请求
我看不出有任何理由拥有多个装货箱。您是否考虑过只使用一个加载框,但可以简单地更改其中显示的内容
$("#loader").ajaxStart(function(){
$(this).show();
}).ajaxStop(function(){
$(this).fadeOut();
});
// ... later on ...
$("#loader span.infotext").text("Loading User Profile, please wait...");
$.ajax({ ... });
ajaxStop
和ajaxStart
是全局事件,在没有其他未决ajax请求的情况下启动ajax请求时,在所有元素上都会触发该事件;在没有其他未决ajax请求的情况下,每次ajax请求结束时,都会触发该事件
我看不出有任何理由拥有多个装货箱。您是否考虑过只使用一个加载框,但可以简单地更改其中显示的内容
$("#loader").ajaxStart(function(){
$(this).show();
}).ajaxStop(function(){
$(this).fadeOut();
});
// ... later on ...
$("#loader span.infotext").text("Loading User Profile, please wait...");
$.ajax({ ... });
我已将其更改为文档
,我的加载消息不会出现。为什么一个绑定必须绑定到一个元素上呢?实际上不,当它们都绑定到文档上时,它仍然不起作用:(谢谢你的建议,但我不明白?你是什么意思,绑定到$({})
。链接也没有显示您的意思。我建议将事件绑定到一个非DOM对象上,这有点笨重和混乱。链接文章中介绍了这一想法,并对其进行了讨论。但是,在进一步考虑后,为什么不只绑定这些事件一次,然后在回调中使用一些条件逻辑来弄清楚该怎么做?我已将其更改为document
,并且我的加载消息没有出现。为什么必须将绑定绑定到元素?实际上,不,当将它们全部绑定到文档时,它仍然不起作用:(感谢您的建议,但我不明白?您是什么意思,绑定到$({})
。链接也没有显示您的意思。我建议将事件绑定到一个非DOM对象上,这有点笨重和混乱。链接文章中介绍了这一想法,并对其进行了讨论。但是,在进一步考虑后,为什么不只绑定这些事件一次,然后在回调中使用一些条件逻辑来想想该怎么办?