Javascript 使用jquery";只删除一个事件处理程序;。关「;

Javascript 使用jquery";只删除一个事件处理程序;。关「;,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我从某处借用了这段代码,以便从一个引导模式切换到另一个引导模式。在切换之后,我想关闭处理程序,这样它就不会每次关闭第一个模式时都切换。我的问题是,一旦使用了模式切换功能,我不知道如何只关闭特定的事件处理程序,而不关闭我在某个模式关闭时触发的另一个事件(在别处完成)。有什么建议吗 //switch modals function switchModals(fromModal, toModal) { $(fromModal).on('hidden.bs.modal', function (e

我从某处借用了这段代码,以便从一个引导模式切换到另一个引导模式。在切换之后,我想关闭处理程序,这样它就不会每次关闭第一个模式时都切换。我的问题是,一旦使用了模式切换功能,我不知道如何只关闭特定的事件处理程序,而不关闭我在某个模式关闭时触发的另一个事件(在别处完成)。有什么建议吗

//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');
}

你需要把这条线放在switchModals之外

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

尝试在
隐藏的.bs.modal
回调中使用
this
而不是
fromModal
,例如

//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');
}

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

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

我找到了解决办法。我只需要给处理程序一个函数所特有的名称空间。然后我通过“.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');
}

那没用。我认为它需要在.off()中的某个地方指定,但我不知道具体是如何指定的。您可以发布完整的代码或网页的链接吗?