twitter-bootstrap 在 Twitter Bootstrap 中,如何从关闭模式对话框中解除事件的绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14451052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Twitter Bootstrap, how do I unbind an event from the closing of a modal dialog?
提问by Spike Williams
I have a function that is bound to the action of hiding a modal dialog.
我有一个函数绑定到隐藏模式对话框的操作。
I'm using code similar to the accepted answer to thisquestion.
我正在使用类似于此问题的已接受答案的代码。
$('#myModal').on('hidden', function () {
// do something…
})
However, this dialog may get reopened for editing, and, when that happens, I don't necessarily want to run this code. Is there a way to "unbind" the function so that it is no longer run when the dialog closes? I haven't found anything in the documentation.
但是,此对话框可能会重新打开以进行编辑,并且在发生这种情况时,我不一定要运行此代码。有没有办法“解除绑定”该函数,以便在对话框关闭时不再运行它?我在文档中没有找到任何内容。
回答by Dennis Rongo
You can do something like to unbind all events tied to the modal element:
您可以执行一些操作,例如解除绑定到模态元素的所有事件:
Unbind all events in the modal:
解除模式中的所有事件的绑定:
/* First option */
$('#myModal').on('hidden', function (e) {
$(e.currentTarget).unbind(); // or $(this)
});
/* Second option is to call it directly when needed */
$('#myModal').unbind();
The Bootrap modal also have specific events tied to it, so you can also specify which event(s) you want to unbind.
Bootrap 模态还具有与之相关的特定事件,因此您还可以指定要解除绑定的事件。
/* Events are 'hidden', 'hide', 'show', 'shown' */
$('#myModal').unbind(/* specific event here */);
If you wish to remove events tied to the content of the modal, you can simply just empty the elements within $('#myModal').empty()and unbind those elements appropriately.
如果您希望删除与模态内容相关的事件,您可以简单地清空其中的元素$('#myModal').empty()并适当地解除这些元素的绑定。
回答by Daniel Kmak
Use:
利用:
$('#myModal').off('hidden');
Why not unbind?
为什么不解绑?
As of jQuery 3.0, .unbind() has been deprecated. It was superseded by the .off() method since jQuery 1.7, so its use was already discouraged.
从 jQuery 3.0 开始,.unbind() 已被弃用。自 jQuery 1.7 起,它被 .off() 方法取代,因此已经不鼓励使用它。
Source: jQuery API.
来源:jQuery API。

