【问题标题】:jquery function executing multiple time for bootstrap modaljquery函数多次执行引导模式
【发布时间】:2015-06-28 08:53:36
【问题描述】:
$(document).on('click', '.event_class', function() {

    $('#originalImageShow').modal('show');

    $('#originalImageShow').on('shown.bs.modal',  function() {
        alert('Modal Open');
    });
    $('#originalImageShow').on('hidden.bs.modal', function() {
        alert('Modal Close');
        $('#originalImageShow').off();
        $(this).removeData('bs.modal');
    });

});

嗨, 每次我单击 event_class 它都会显示两次“模式打开”警报。甚至有时它会显示多次。而且当我关闭引导模式时,有时它会多次显示“模式关闭”警报。 基本上警报号码的数量是随机变化的。 请帮忙。

【问题讨论】:

  • 将您的代码放入 jsfiddle.net (html/css/js) 中,以便我们“实时”检查它。请确保您只放了显示问题的基本代码。没有人愿意翻阅数百行什么都不做的代码。
  • @Dekel 谢谢。让我告诉你我真正的问题。请访问ourbazaar.net/cropperTest.html 点击图片,旋转它。它工作正常,但是如果您关闭模态打开另一个图像并再次尝试旋转,它将一次单击旋转两次,关闭模态并通过单击图像打开另一个模态尝试旋转它将旋转三倍,放大的情况相同并缩小。我认为问题出在bs-modal上。您可以从gist.github.com/muradhassan/1693f614e72ac015d6fd 获取代码。我无法使用 jsfiddle,因为图像发生跨域资源问题

标签: jquery twitter-bootstrap bootstrap-modal


【解决方案1】:

这是因为您在 click 函数中添加了 shown.bs.modal ,该函数将在每次创建模态时绑定事件!

查看以下代码

$('.launchConfirm').on('click', function (e) {
    $('#confirm')
        .modal({ backdrop: 'static', keyboard: false })
        
});

$('.modal').on('shown.bs.modal',  function() {
        alert('Modal Open');
    });

$('.modal').on('hidden.bs.modal', function() {
        alert('Modal Close');
    });
body,
.modal-open .page-container,
.modal-open .page-container .navbar-fixed-top,
.modal-open .modal-container {
	overflow-y: scroll;
}

@media (max-width: 979px) {
	.modal-open .page-container .navbar-fixed-top{
		overflow-y: visible;
	}
}
<link href="http://jschr.github.io/bootstrap-modal/css/bootstrap-modal.css" rel="stylesheet"/>
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap-responsive.css" rel="stylesheet"/>
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>
<script src="http://jschr.github.io/bootstrap-modal/js/bootstrap-modal.js"></script>
<script src="http://jschr.github.io/bootstrap-modal/js/bootstrap-modalmanager.js"></script>



<div class="page-container">
    <div class="container">
        <br />
        <button class="btn launchConfirm">Launch Confirm</button>
    </div>
</div>

<div id="confirm" class="modal hide fade">
  <div class="modal-body">
    Do you want to continue?
  </div>
  <div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn btn-primary" data-value="1">Continue</button>
    <button type="button" data-dismiss="modal" class="btn" data-value="0">Cancel</button>
  </div>
</div>

【讨论】:

  • 谢谢@Pratik Shah 但我需要在点击事件中使用showed.bs.modal shown.bs.modal 让我向您展示我真正的问题。请访问ourbazaar.net/cropperTest.html 点击图片,旋转它。它工作正常但是如果您关闭模态打开另一个图像并尝试再次旋转它将一次单击旋转两次,关闭模态并通过单击图像打开另一个模态尝试旋转它将旋转三倍,放大的情况相同并缩小。我认为问题在于引导模式隐藏。您可以从 gist.github.com/muradhassan/1693f614e72ac015d6fd 获取代码
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 2014-09-08
  • 2018-06-05
  • 1970-01-01
相关资源
最近更新 更多