Bootstrap: Unwanted accordion behaviour

隐身守侯 提交于 2021-01-29 11:48:56

问题


The following code works fine....except.... I don't want to expand or callapse a row when the user clicks a checkbox. How can I block this? (I have jquery plugged in. I'm wondering if I can use this to solve the problem?)

<table class="table table-hover">
    <thead>
        <td>
            <h5>Select All:</h5>
        </td>
        <td>
            <h5><input type="checkbox" id="checkAll"></h5>
        </td>
    </thead>

    <tbody>
        <tr>
            <td colspan="2"> <input type="submit" , value="Replace Selected"> </td>
        </tr>
        <tr class="accordion-toggle" data-toggle="collapse" data-target="#tgt1">

            <td> Heading </td>
            <td><input type="checkbox" value="92340332" name="c1"></td>
        </tr>
        <tr>
            <td colspan="2">
                <div id="tgt1" class="collapse">content content</div>
            </td>
        </tr>
        <tr class="accordion-toggle" data-toggle="collapse" data-target="#tgt2">
            <td>Heading</td>
            <td><input type="checkbox" value="146735230" name="c2"></td>
        </tr>
        <tr>
            <td colspan="2">
                <div id="tgt2" class="collapse">content content</div>
            </td>
        </tr>
    </tbody>
</table>

回答1:


You'll need to use e.stopPropagation(); to your checkbox to prevent.

 $("myElement").on('click', function (e) { e.preventDefault(); })

or

$('input').click(function(e) {
    e.stopPropagation(); // prevents event e from going to parent
});


来源:https://stackoverflow.com/questions/55528360/bootstrap-unwanted-accordion-behaviour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!