Bootstrap 3 - 如果手风琴在模态内,则不会触发事件shows.bs.collapse或shown.bs.modal

时间:2017-01-18 16:47:41

标签: javascript jquery html css twitter-bootstrap

<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 id="modal-title"> </h4>
    </div>
    <div class="modal-body">
      <div class="panel-group" id="accordion">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                            <a  class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse1">Course Introduction

                            </a>
                            </h4>
                        </div>

                        <div id="collapse1" class="panel-collapse collapse in">
                            <div class="panel-body">
                            <p>abc</p>
                            </div>
                        </div>
                    </div>

                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse2">Description

                            </a>
                            </h4>
                        </div>

                        <div id="collapse2" class="panel-collapse collapse">
                            <div class="panel-body">
                            <p>Lorem </p>
                            </div>
                        </div>
                    </div>

                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse3">Goals

                            </a>
                            </h4>
                        </div>

                        <div id="collapse3" class="panel-collapse collapse">
                            <div class="panel-body">
                            <p>Lorem ipsum dolor sit </p>
                            </div>
                        </div>
                    </div>

                </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
  </div></div> <!-- end of Modal --> 

这是javascript。我只想在手风琴中点击一个标签时将类添加到可折叠标签,并希望在模态关闭时关闭所有手风琴但是事件shown.bs.collapse或shown.bs.modal没有触发。

$('#myModal').on('hidden.bs.modal', function () {
$('.collapse').collapse('hide');
});
$('#myModal').on('shown.bs.modal', function () {
$('#collapse1').collapse('show');
});

$('#accordion').find('.panel-default:has(".in")').addClass('panel-danger');

$('#accordion').on('show.bs.collapse', function (e) {
$(e.target).closest('.panel-default').addClass(' panel-danger');
}).on('hide.bs.collapse', function (e) {
$(e.target).closest('.panel-default').removeClass(' panel-danger');
})

2 个答案:

答案 0 :(得分:2)

最后我解决了这个问题。我将我的jquery代码放入

$(文件).ready(function(){ ................ CODE ............的});

答案 1 :(得分:1)

您忘记在代码中使用该事件。它应该是db.collectionname.update( { "field" : "oldvalue" }, { $set:{ "field" : "newvalue" } }, { multi : true } ); 而不是$('#myModal').on('click','hidden.bs.modal', function () {。 这是工作示例......

$('#myModal').on('hidden.bs.modal', function () {
$('#myModal').on('click','hidden.bs.modal', function () {
$('.collapse').collapse('hide');
});
$('#myModal').on('click','shown.bs.modal', function () {
$('#collapse1').collapse('show');
});

$('#accordion').find('.panel-default:has(".in")').addClass('panel-danger');

$('#accordion').on('click','show.bs.collapse', function (e) {
$(e.target).closest('.panel-default').addClass(' panel-danger');
}).on('hide.bs.collapse', function (e) {
$(e.target).closest('.panel-default').removeClass(' panel-danger');
})