twitter-bootstrap 引导远程模式删除javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19241818/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:47:56  来源:igfitidea点击:

bootstrap remote modal remove javascript

jquerytwitter-bootstrapbootstrap-modal

提问by user1857519

i have modals in my bootstrap application. the modals get their content from a dataremote source. In this modal I defined some javascript functions.

我的引导程序应用程序中有模态。模态从 dataremote 源获取它们的内容。在这个模式中,我定义了一些 javascript 函数。

my problem is, when i close the modal and open it again the javascript code is available twice. That means the on click event will be performed multiple times.

我的问题是,当我关闭模式并再次打开它时,javascript 代码可用两次。这意味着点击事件将被执行多次。

Is there a possibility to clear the modals javascript code on hide?

是否有可能清除隐藏时的模态 javascript 代码?

I also use

我也用

$('body').on('hidden', '.modal', function () {
  $(this).removeData('modal');
});

to prevent from showing the same content again

防止再次显示相同的内容

回答by davidkonrad

Here is an example :

这是一个例子:

markup :

标记:

<button type="button" id="test">Launch modal</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog"></div>
</div>

script :

脚本 :

$('#test').click(function() {
    $("#myModal").modal({
        remote : 'your-remote-href'
    });
});
//target this event, see http://getbootstrap.com/javascript/#modals -> events
$('#myModal').on('hidden.bs.modal', function () {
    $(this).empty(); //<---- empty() to clear the modal
})

I assumed you are not using the href-attribute

我假设你没有使用href-attribute

<button type="button" data-toggle="modal" href="some href" data-target="#myModal">Launch modal</button>

Doing that the modal content is only loaded once. and emptying the modal will be permanent.

这样做模态内容只加载一次。并且清空模态将是永久性的。