简体   繁体   English

具有内部所示手风琴的自举式手风琴,具有不同的所示bs.collapse事件

[英]bootstrap accordions with inner accordion with different shown.bs.collapse events

I have a bootstrap accordion which has an inner accordion. 我有一个带有内部手风琴的自举手风琴。 The issue I'm encountering is that when i click on the inner accordion the 'shown.bs.collapse' event which the outer accordion is binded to is being triggered which shouldnt be the case, and i would like to have the inner panel binded to its own shown.bs.collapse event if that is possible? 我遇到的问题是,当我单击内部手风琴时,会触发外部手风琴绑定到的“ shown.bs.collapse”事件,这种情况不应该发生,我想绑定内部面板到它自己的show.bs.collapse事件,如果可能的话?

To give you a better idea I have the following:- 为了给您一个更好的主意,我有以下几点:

$('#accordion').on('shown.bs.collapse', function (e) {

    GetData(param1, param2);
})

The GetData is aa function which does an ajax call and appends an inner accordion to the outer accordion. GetData是一个执行ajax调用并将内部手风琴附加到外部手风琴的函数。 The outer accordion id is #accordion. 外部手风琴ID为#accordion。 That works fine, however when i click on the inner accordion which i gave it an id of #accordion2, the above code is being called again. 效果很好,但是当我单击内部手风琴时,我给它赋予了ID#accordion2,上面的代码被再次调用。 Can someone please point me in right direction? 有人可以指出正确的方向吗?

To better understand the structure of the accordion...I have an mvc view with the following:- 为了更好地理解手风琴的结构...我有一个带有以下内容的mvc视图:-

    <div id="accordion" class="panel-group accordion" style="margin-top:20px">

    </div>

Then i construct the rest dynamically through javascript functions. 然后,我通过javascript函数动态构建其余部分。 The following function creates the outer accordion panels: 以下功能创建外部手风琴面板:

function GetGroups() { 函数GetGroups(){

$.ajax({
    url: url,
    dataType: "JSON",
    contentType: "application/json; charset=utf-8",
    success: function (data) {

        $.each(data.data, function (index, item) {

            var collapseContainer = 'collapse' + accordingIndexes[index];
            var mkContainer = 'testContainer' + index;

            $("#accordion").append('<div class="panel panel-default">\
        <div class="panel-heading ">\
            <h4 class="panel-title">\
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion"  href="#' + collapseContainer + '">'
                   + item.Name + ' - ' + item.Value +
                '</a>\
            </h4>\
        </div>\
        <div id="' + collapseContainer + '" class="panel-collapse collapse">\
            <div id="' + mkContainer + '" class="panel-body border-red" data-source-id="' + item.Id + '">\
            </div>\
        </div>\
    </div>');

        });
    }
});

} }

and the GetData function which is called in 'shown.bs.collapse' looks like this: 在“ shown.bs.collapse”中调用的GetData函数如下所示:

function GetData(testContainer) { 函数GetData(testContainer){

$.ajax({
    url: url,
    dataType: "JSON",
    contentType: "application/json; charset=utf-8",
    success: function (data) {

        $.each(data.data, function (index, item) {

            var collapseContainer = 'collapseInner' + accordingIndexes[index];

            $("#" + testContainer).append('<div class="accordion-inner">\
                <div class="accordion" id="accordion2">\
                <div class="panel panel-default">\
        <div class="panel-heading ">\
            <h4 class="panel-title">\
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" data-source-id="' + item.Id + '" href="#' + collapseContainer + '">'
                   + item.SourceName + 
                '</a>\
            </h4>\
        </div>\
        <div id="' + collapseContainer + '" class="panel-collapse collapse">\
            <div class="panel-body border-red">\
                    TEST\
            </div>\
        </div>\
    </div>\
    </div>\
    </div>');

        });
    }
});

} }

Now when i tried binding #accordion2 to its own event, it was not triggered, the one of #accordion was triggered. 现在,当我尝试将#accordion2绑定到它自己的事件时,没有触发它,而是触发了#accordion之一。 Any ideas please? 有什么想法吗?

$('#accordion2').on('shown.bs.collapse', function (e) {

    console.log('test2');
})

Since your #accordion2 was added dynamically, you need something called event delegation here, and so you might need to use below code: 由于#accordion2是动态添加的,因此您在这里需要一个称为event delegation东西,因此您可能需要使用以下代码:

$(document).on('shown.bs.collapse', '#accordion2', function (e) {
  console.log('test2');
})

You can replace $(document) with some nearest parent element of #accordion2 which exists on page load and is not loaded through ajax 您可以将$(document)替换$(document) #accordion2某个最接近的父element ,该elementpage load时就存在,并且不会通过ajax加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bootstrap 3-如果手风琴位于模式中,则不会触发showd.bs.collapse或showd.bs.modal事件 - Bootstrap 3 - Event shown.bs.collapse or shown.bs.modal not fired if the accordion is inside a modal 如何响应“ shown.bs.collapse”事件? - How to respond to “shown.bs.collapse” event? 获取在hidden / shown.bs.collapse上折叠的元素 - Get the element that was collapsed on hidden/shown.bs.collapse 多个自举手风琴在单击一个手风琴时崩溃,其他折叠 - Multiple bootstrap accordions with collapse other when clicked on one accordion 尝试通过自举崩溃构建多个手风琴。 单击第二个手风琴会使第一个折叠 - Trying to build multiple accordions with bootstrap collapse. Clicking on a second accordion collapses the first 将内部手风琴制作成空的手风琴 - Creating inner-accordions into an empty accordion Bootstrap-折叠所有手风琴并单击显示 - Bootstrap - Collapse all accordions and show clicked Bootstrap崩溃 - 同时打开多个手风琴 - Bootstrap collapse - opening multiple accordions at the same time Bootstrap 在按钮单击时折叠所有手风琴 - Bootstrap collapse all accordions on button click Bootstrap 3 - 如果手风琴在通过ajax填充的模态中,则不会触发事件hidden.bs.collapse - Bootstrap 3 - Event hidden.bs.collapse not fired if the accordion is inside a modal populated via ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM