单击时,Bootstrap下拉搜索栏关闭

时间:2014-08-14 14:21:23

标签: javascript jquery html css twitter-bootstrap

我的网站上有一个下拉搜索栏,点击后会关闭。 我试图阻止这种情况,但我自己无法找到它。

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" 
            data-target="#navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand logo" href="#">BrandName</a>
    </div>

  <div class="collapse navbar-collapse" id="navbar-collapse">
      <ul class="nav navbar-nav">
        <li><a href="#">link1</a></li>
        <li><a href="#">link2</a></li>

      </ul>

      <button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle hidden-xs pull-right" ><span class="glyphicon glyphicon-search"></span> </button>
        <div class="dropdown-menu pull-right">
          <form class="navbar-form pull-right " role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Zoeken">
        </div>
        </div>

      <div class="search-phone visible-xs" >
      <form class="navbar-form  " role="search">
        <div class="form-group search-phone2">
          <input type="text" class="form-control" placeholder="Zoeken">
          <button type="button" class="btn btn-default" ><span class="glyphicon glyphicon-search"></span>
           </button> 
        </div>      
      </form>
      </div>

    </div>
  </div> <!-- container-fluid -->
</nav>

我尝试过这样的不同之处: 我不知道这是不是最好的方式。

<script>

    $('.input.form-control').click(function(e) {
        e.stopPropagation();
        });

    </script>

但我无法找到解决方案。

2 个答案:

答案 0 :(得分:1)

请改用以下内容:

   $('input.form-control').click(function(e) {
        e.stopPropagation();
        });

使用JSFiddle:http://jsfiddle.net/oc60jLdb/

答案 1 :(得分:1)

好的,我设法让它工作,没有涉及任何JavaScript。语法引导需要UL&gt; LI下拉列表,您在其中添加了表单而不是按钮和DIV。你似乎在两次添加搜索,但在我的例子中,没有必要这样做。下拉/搜索表单随后调整大小调整,虽然一些媒体查询可以修复显示移动分辨率的水平scroolbar。

Bootply Example

HTML:

<nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Brand</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
            </ul>

            <ul class="nav navbar-nav navbar-right">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-search"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li>
                          <form class="navbar-form navbar-left" role="search">
                              <div class="form-group">
                              <input type="text" class="form-control" placeholder="Search">
                              </div>
                              <button type="submit" class="btn btn-default btn-block submitBtn">Submit</button>
                          </form>
                        </li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

的CSS:

.submitBtn{
    margin-top:5px;
    background:#E7E7E7;
}

.submitBtn:hover{
    background:#DADADA;
}

@media only screen and (max-width : 767px) {
  .navbar-form{
    margin-right:0;
    margin-left:0;
  }
}