1

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

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

是否有可能在隐藏时清除模态 JavaScript 代码?

我也用

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

防止再次显示相同的内容

4

1 回答 1

1

这是一个例子:

标记:

<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>

脚本 :

$('#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
})

我假设你没有使用href-attribute

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

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

于 2013-10-09T15:12:58.830 回答