如何一次打开一个手风琴选项卡

时间:2018-09-01 06:43:34

标签: twitter-bootstrap forms accordion

我在尝试获取它时遇到了麻烦,因此我的Bootstrap手风琴风格表格一次只能打开一个步骤标签。我需要更改什么才能一次只打开一个选项卡?

这是指向完整内容的CodePen链接。 这是我的HTML的一部分:

<div id="accordion" role="tablist" aria-multiselectable="false">
<div class="card">
    <div class="card-header" role="tab" id="headingOne">
        <div class="mb-0">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
                <h3>Step 1 of 4: Create Profile</h3>
                <div class="edit-step">Edit Step</div>
            </a>
        </div>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne" aria-expanded="false" style="">
        <div class="card-block">
            <form>
                <h1>Organization</h1>
                <label>Membership Category</label>
                <select id="category">
                    <option value=""></option>
                    <option value="CM">Company</option>
                    <option value="NS">Educational</option>
                    <option value="GM">Government</option>
                    <option value="OM">Organizational</option>
                    <option value="IND">Individual</option>
                    <option value="RET">Retail</option>
                </select><br><br>
                <div id="continue1" class="continue-button">Continue</div><br>
            </form>
        </div>
    </div>
</div>
<div class="card">
    <div class="card-header" role="tab" id="headingTwo">
        <div class="mb-0">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
                <h3>Step 2 of 4: Payment Information</h3>
                <div class="edit-step">Edit Step</div>
            </a>
        </div>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo" aria-expanded="false">
        <div class="card-block">
            <form>
                <h1>Organization</h1>
                <br><br>
                <div id="continue2" class="continue-button">Continue</div><br>
            </form>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

由于您已经在项目中使用jQuery,因此只需选择类panel-collapse collapse in的所有div并删除类in的div

Codepen:https://codepen.io/anon/pen/yxMyqY

$(document).ready(function(){
    $("[data-parent='#accordion']").on("click", function(){
        var trigger = $(this);
        $(".panel-collapse.collapse.in").each(function(){
            if( trigger.attr("href") != ("#"+$(this).attr("id")) ){
            $(this).removeClass("in");
            } // condition returns false on iteration on div to be opened
        });   
    });
});