当我点击bootstrap中的下拉菜单时它会打开然后自动关闭

时间:2016-08-31 05:28:02

标签: javascript php html css twitter-bootstrap

body {
    padding-top: 50px;
}
.dropdown.dropdown-lg .dropdown-menu {
    margin-top: -1px;
    padding: 6px 20px;
}
.input-group-btn .btn-group {
    display: flex !important;
}
.btn-group .btn {
    border-radius: 0;
    margin-left: -1px;
}
.btn-group .btn:last-child {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}
.form-horizontal .btn[type="submit"] {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
.form-horizontal .form-group {
    margin-left: 0;
    margin-right: 0;
}
.form-group .form-control:last-child {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

@media screen and (min-width: 768px) {
    #adv-search {
        width: 500px;
        margin: 0 auto;
    }
    .dropdown.dropdown-lg {
        position: static !important;
    }
    .dropdown.dropdown-lg .dropdown-menu {
        min-width: 500px;
    }
}
<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.min.css">
		<link rel="stylesheet" href="css/style.css">
		<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
		<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
	</head>
<body>
<div class="container">
	<div class="row">
		<div class="col-md-12">
			<form class="form-horizontal" role="form">
				<div class="input-group" id="adv-search">
					<input type="text" class="form-control" placeholder="Search for snippets" />
					<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>
								<div class="dropdown-menu dropdown-menu-right" role="menu">
									<div class="form-group">
										<label for="filter">Filter by</label>
										<select class="form-control">
											<option value="0" selected>All Snippets</option>
											<option value="1">Featured</option>
											<option value="2">Most popular</option>
											<option value="3">Top rated</option>
											<option value="4">Most commented</option>
										</select>
									</div>
									<div class="form-group">
										<label for="contain">Author</label>
										<input class="form-control" type="text" />
									</div>
									<div class="form-group">
										<label for="contain">Contains the words</label>
										<input class="form-control" type="text" />
									</div>
									<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
								</div>
							</div>
							<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
						</div>
					</div>
				</div>
			</form> 
		</div>
	</div>
</body>

我正在尝试为php项目创建高级搜索。 但我的问题是,当我点击内部下拉菜单 - 它打开然后我释放鼠标按钮它会自动关闭。

我正在使用bootstrap 3.3.6。

body {
    padding-top: 50px;
}
.dropdown.dropdown-lg .dropdown-menu {
    margin-top: -1px;
    padding: 6px 20px;
}
.input-group-btn .btn-group {
    display: flex !important;
}
.btn-group .btn {
    border-radius: 0;
    margin-left: -1px;
}
.btn-group .btn:last-child {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}
.form-horizontal .btn[type="submit"] {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
.form-horizontal .form-group {
    margin-left: 0;
    margin-right: 0;
}
.form-group .form-control:last-child {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

@media screen and (min-width: 768px) {
    #adv-search {
        width: 500px;
        margin: 0 auto;
    }
    .dropdown.dropdown-lg {
        position: static !important;
    }
    .dropdown.dropdown-lg .dropdown-menu {
        min-width: 500px;
    }
}
<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.min.css">
		<link rel="stylesheet" href="css/style.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>

<!-- begin snippet: js hide: false console: true babel: false -->

    </head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <form class="form-horizontal" role="form">
                <div class="input-group" id="adv-search">
                    <input type="text" class="form-control" placeholder="Search for snippets" />
                    <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>
                                <div class="dropdown-menu dropdown-menu-right" role="menu">
                                    <div class="form-group">
                                        <label for="filter">Filter by</label>
                                        <select class="form-control">
                                            <option value="0" selected>All Snippets</option>
                                            <option value="1">Featured</option>
                                            <option value="2">Most popular</option>
                                            <option value="3">Top rated</option>
                                            <option value="4">Most commented</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label for="contain">Author</label>
                                        <input class="form-control" type="text" />
                                    </div>
                                    <div class="form-group">
                                        <label for="contain">Contains the words</label>
                                        <input class="form-control" type="text" />
                                    </div>
                                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
                                </div>
                            </div>
                            <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
                        </div>
                    </div>
                </div>
            </form> 
        </div>
    </div>
</body>

2 个答案:

答案 0 :(得分:0)

我使用id'someyourContainer'命名了以下div。

<div class="dropdown-menu dropdown-menu-right" role="menu" id="someyourContainer">

按钮的切换事件表现为下拉列表,强制关闭内部点击下拉菜单。单击内部下拉列表时限制关闭事件,如下所示

 <script type="text/javascript">
        $('#someyourContainer').click(function (e) {
            e.stopPropagation();
        });
    </script> 

答案 1 :(得分:0)

事件冒泡是那里的主要罪魁祸首,您可以使用event.stopPropagation();为展开的dropdown-menu停止此操作,并在点击展开的下拉列表时自动关闭。

&#13;
&#13;
$('.dropdown-menu').on('click', function(event) {
  event.stopPropagation();
});
&#13;
body {
  padding-top: 50px;
}
.dropdown.dropdown-lg .dropdown-menu {
  margin-top: -1px;
  padding: 6px 20px;
}
.input-group-btn .btn-group {
  display: flex !important;
}
.btn-group .btn {
  border-radius: 0;
  margin-left: -1px;
}
.btn-group .btn:last-child {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}
.form-horizontal .btn[type="submit"] {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
.form-horizontal .form-group {
  margin-left: 0;
  margin-right: 0;
}
.form-group .form-control:last-child {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
@media screen and (min-width: 768px) {
  #adv-search {
    width: 500px;
    margin: 0 auto;
  }
  .dropdown.dropdown-lg {
    position: static !important;
  }
  .dropdown.dropdown-lg .dropdown-menu {
    min-width: 500px;
  }
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>



<div class="container">
  <div class="row">
    <div class="col-md-12">
      <form class="form-horizontal" role="form">
        <div class="input-group" id="adv-search">
          <input type="text" class="form-control" placeholder="Search for snippets" />
          <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>
                <div class="dropdown-menu dropdown-menu-right" role="menu">
                  <div class="form-group">
                    <label for="filter">Filter by</label>
                    <select class="form-control">
                      <option value="0" selected>All Snippets</option>
                      <option value="1">Featured</option>
                      <option value="2">Most popular</option>
                      <option value="3">Top rated</option>
                      <option value="4">Most commented</option>
                    </select>
                  </div>
                  <div class="form-group">
                    <label for="contain">Author</label>
                    <input class="form-control" type="text" />
                  </div>
                  <div class="form-group">
                    <label for="contain">Contains the words</label>
                    <input class="form-control" type="text" />
                  </div>
                  <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                  </button>
                </div>
              </div>
              <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span>
              </button>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
&#13;
&#13;
&#13;