如何以编程方式打开Bootstrap下拉列表

编程入门 行业动态 更新时间:2024-10-08 22:57:40
本文介绍了如何以编程方式打开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')不是 $('。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下拉列表

本文发布于:2023-11-05 02:54:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   列表   Bootstrap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!