2016-07-04 57 views
1

我从某处借用此代码以从一个引导模式切换到另一个模式。切换后,我想关闭处理程序,以便每次只关闭第一个模式时都不会切换。我的问题是,一旦使用了模式切换功能,我不知道如何关闭特定事件处理程序,而不关闭在特定模式关闭时执行的其他事件(在其他位置完成)时如何关闭。有什么建议么?使用jquery删除一个事件处理程序“.off”

//switch modals 
function switchModals(fromModal, toModal) { 
    $(fromModal).on('hidden.bs.modal', function (e) { 
     $(toModal).modal('show'); 
     //clear this function so it doesn't show up if they exit the window again 
     $(fromModal).off('hidden.bs.modal'); 
    }); 
    $(fromModal).modal('hide'); 
} 

回答

0

我找到了解决办法。我只需要为处理程序提供一个函数特有的名称空间。然后我通过它的命名空间在“.off()”方法中解除绑定。我选择了“switch”作为命名空间。

//switch modals 
function switchModals(fromModal, toModal) { 
    $(fromModal).on('hidden.bs.modal.switch', function (e) { 
     $(toModal).modal('show'); 
     //clear this function so it doesn't show up if they exit the window again 
     $(fromModal).off('.switch'); 
    }); 
    $(fromModal).modal('hide'); 
} 
1

你需要把该行的switchModals外

$(<FromModalId>).modal('hide'); 
0

尝试使用this代替fromModalhidden.bs.modal回调,例如内

//switch modals 
function switchModals(fromModal, toModal) { 
    $(fromModal).on('hidden.bs.modal', function (e) { 
     $(toModal).modal('show'); 
     //clear this function so it doesn't show up if they exit the window again 
     $(this).off('hidden.bs.modal'); 
    }); 
    $(fromModal).modal('hide'); 
} 
+0

这没有奏效。我认为它需要在.off()的某处指定,但我不知道如何。 – Quin

+0

你可以发布你的完整代码,或链接到你的网页? –

0

请尝试以下方法模态之间切换:

var el = fromModal+','+toModal; 
    $(el).on('hidden.bs.modal', function (e) { 
     $(el).not(this).modal('show'); 
     $(this).modal('hide'); 
    });