如何以编程方式打开Bootstrap下拉列表 [英] How to open Bootstrap dropdown programmatically
本文介绍了如何以编程方式打开Bootstrap下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我点击另一个下拉列表的项目时,我正在尝试打开Bootstrap下拉列表。
I'm trying to open a Bootstrap dropdown when I click a item of another dropdown.
想法是从第一个下拉列表中选择一个城市 - 然后该脚本将自动打开带有区域的第二个下拉列表(并仅显示与所选城市相对应的区域)。
The idea is to select a city from the first drop down - then the script will auto open the second dropdown with areas (and show only areas corresponding to the chosen city).
这是我的JS:
$('#sidebar_filter_city li').click(function(){
$('#sidebar_filter_areas').dropdown('toggle');
});
这是HTML:
<div class="dropdown form-control">
<div data-toggle="dropdown" id="sidebar_filter_cities" class="sidebar_filter_menu" data-value="jersey-city">Jersey City<span class="caret caret_sidebar"></span></div>
<input type="hidden" name="advanced_city" value="jersey-city">
<ul id="sidebar_filter_city" class="dropdown-menu filter_menu" role="menu" aria-labelledby="sidebar_filter_cities">
<li role="presentation" data-value="">All Cities</li>
<li role="presentation" data-value="jersey-city">Jersey City</li>
<li role="presentation" data-value="london">London</li>
<li role="presentation" data-value="new-york">New York</li>
</ul>
</div>
</div>
<div class="dropdown form-control">
<div data-toggle="dropdown" id="sidebar_filter_areas" class="sidebar_filter_menu">All Areas<span class="caret caret_sidebar"></span> </div>
<input type="hidden" name="advanced_area" value="">
<ul id="sidebar_filter_area" class="dropdown-menu filter_menu" role="menu" aria-labelledby="sidebar_filter_areas">
<li role="presentation" data-value="">All Areas</li>
<li role="presentation" data-value="east-harlem" data-parentcity="">East Harlem</li>
<li role="presentation" data-value="greenville" data-parentcity="">Greenville</li>
<li role="presentation" data-value="manhattan" data-parentcity="">Manhattan</li>
<li role="presentation" data-value="northern-brooklyn" data-parentcity="">Northern Brooklyn</li>
.....
</ul>
</div>
</div>
推荐答案
最好的方法是检查下拉列表是否不是已打开,然后使用 .dropdown('toggle')
。
The best way is to check if the dropdown is not already open, and then to use .dropdown('toggle')
.
要注意的事项:
- 如果你想触发它通过点击另一个元素,你必须
杀死另一个元素上的click事件 - 否则Bootstrap将
视为下拉列表外的点击并立即关闭它。 / li>
-
$('。dropdown')。addClass('open')
不是
<$ c的好替代品$ c> $('。dropdown-toggle')。dropdown('toggle')按照其他
答案中的建议,因为它会导致下拉列表永久保持打开
而不是在单击组件时关闭。
- If you want to trigger it by clicking on another element, you must kill the click event on the other element- otherwise Bootstrap will treat it as a click outside of the dropdown and immediately close it.
$('.dropdown').addClass('open')
is not a good replacement for$('.dropdown-toggle').dropdown('toggle')
as suggested in other answers, because it will cause the dropdown to stay permanently open instead of closing when you click off of the component.
HTML:
<button class="btn btn-secondary trigger_button">Trigger dropdown</button><br><br>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown">
Dropdown
</button>
<div class="dropdown-menu">
Stuff...
</div>
</div>
JS:
$('.trigger_button').click(function(e){
// Kill click event:
e.stopPropagation();
// Toggle dropdown if not already visible:
if ($('.dropdown').find('.dropdown-menu').is(":hidden")){
$('.dropdown-toggle').dropdown('toggle');
}
});
这篇关于如何以编程方式打开Bootstrap下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文