Bootstrap 3 - 如果手风琴在通过ajax填充的模态中,则不会触发事件hidden.bs.collapse

时间:2015-10-14 17:12:37

标签: javascript jquery twitter-bootstrap accordion bootstrap-modal

我有一个简单的bootstrap模式通过ajax填充。内容是由bootstrap插件Collapse制作的Accordion。如果我尝试检测hidden.bs.collapse(或任何其他Event detectable),它就不会被解雇。

http://jsfiddle.net/Diac/n2jm5cy8/

HTML

<!-- Button trigger modal -->
<a href="/echo/html/" data-remote="false" data-toggle="modal" data-target="#myModal">Launch demo modal</a>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body" id="modalbody">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

accordion.html(通过ajax加载)

<div class='panel-group' id='accordion'>
    <div class='panel panel-default' id='panel1'>
        <div class='panel-heading'>
            <h4 class='panel-title'>
                <a data-toggle='collapse' data-parent='#accordion' href='#collapseOne'>Collapsible Group Item #1</a>
            </h4>
        </div>
        <div id='collapseOne' class='panel-collapse collapse in'>
            <div class='panel-body'> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div>
        </div>
    </div>
</div>

的JavaScript

$('a[data-toggle]').click(function(e) {
    e.preventDefault();
});

$('#myModal').on('show.bs.modal', function(event) {
    $.ajax({
        url: "accordion.html",
        success: function(responseText) {
            $("#modalbody").html(responseText);
        }
    });
});

$('.panel').on('hidden.bs.collapse', function (e) {
    alert('Event fired on #' + e.currentTarget.id);
})

1 个答案:

答案 0 :(得分:5)

在DOM中存在.panel类的任何元素之前,您尝试将事件侦听器附加到具有.panel类的元素。

为了使您的代码正常工作,您可以在.panel实际插入DOM之后,将$('.panel').on(...)移动到AJAX成功回调中。或者,也许最好是,您可以改为使用事件委托:

$('#modalbody').on('hidden.bs.collapse', '.panel', function (e) {
    alert('Event fired on #' + e.currentTarget.id);
});

请参阅jQuery documentation中有关事件委派的讨论。

相关摘录:

  

事件处理程序仅绑定到当前选定的元素;它们必须在您的代码调用.on()时存在。要确保元素存在且可以选择,请将脚本放在HTML标记中的元素之后,或者在文档就绪处理程序中执行事件绑定。或者,使用委派事件来附加事件处理程序。