下拉菜单在css中崩溃

时间:2016-04-15 07:14:38

标签: javascript jquery html css twitter-bootstrap-3

我正在使用

中的代码段

http://bootsnipp.com/snippets/featured/advanced-dropdown-search

我对代码进行了以下更改

 <div class="col-md-12">
            <form action="./" method="POST" autocomplete="on">
                <div class="input-group" id="adv-search">
                    <input type="text" class="form-control"
                        placeholder="Search for snippets" id="mainForm" name="searchBox" />
                    <div class="input-group-btn">
                        <div class="btn-group" role="group">
                            <div class="dropdown dropdown-lg">
                                <button type="button" class="btn btn-default dropdown-toggle"
                                    data-toggle="dropdown" aria-expanded="false">
                                    <span class="caret"></span>
                                </button>
                                <span class="dropdown-menu dropdown-menu-right" role="menu">
                                    <div class="form-horizontal" role="form">
                                        <div class="form-group">
                                            <label for="filter">Filter by</label> <select
                                                class="form-control" name="docType">
                                                <option value="0" selected>All Sources</option>
                                                <option value="1">Option 1</option>
                                                <option value="2">Option 2</option>

                                            </select>
                                        </div>
                                        <div class="form-group">
                                            <label for="contain">Author / Modifier</label> <input
                                                class="form-control" type="text" name="authorName" />
                                        </div>
                                        <div class="form-group">
                                            <label for="contain">Contains the words</label> <input
                                                class="form-control" type="text" name="words" />
                                        </div>
                                        <!-- <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button> -->
                                    </div>
                                </span>
                            </div>
                            <button type="submit" class="btn btn-primary">
                                <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                            </button>
                        </div>
                    </div>
                </div>
            </form>
        </div>

在移动上面的表单标记后,只需点击下拉列表中的任意位置即可折叠下拉菜单。

有人可以解释一下原因吗?

我尝试做了很多改动,但对我没什么用。

https://jsfiddle.net/tj2y5ptp/

1 个答案:

答案 0 :(得分:4)

尝试删除data-toggle="dropdown"并使用jquery .toggleClass('open');.removeClass('open');open/close下拉菜单(然后只需点击外部的下拉菜单即可关闭nenu正文)

打开下拉列表:

$('.dropdown-lg .btn').on('click', function (event) {
    $(this).parent().toggleClass('open');
});

关闭下拉菜单(点击正文时):

$('body').on('click', function (e) {
    if (!$('.dropdown-lg').is(e.target) 
        && $('.dropdown-lg').has(e.target).length === 0 
        && $('.open').has(e.target).length === 0
    ) {
        $('.dropdown-lg').removeClass('open');
    }
});

请参阅Updated fiddle我希望它对您有所帮助,谢谢。