【发布时间】:2021-02-16 19:30:26
【问题描述】:
我正在尝试让我的 BS4 下拉菜单显示用户在下拉切换按钮中选择的值,即.dropdown-toggle
这个问题之前已经问过here,但它有点过时了,因为它使用了 BS3 框架。它也没有提供多次使用下拉菜单的方法。
我的jsFiddle 在这里显示了我目前面临的问题,以及我如何调整之前链接的解决方案。
BS4 下拉菜单
<div class="btn-group">
<button class="btn btn-sm dropdown-toggle px-0 mr-3" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Sort by:
</button>
<div class="dropdown-menu">
<a class="dropdown-item active" href="#">Most Recent</a>
<a class="dropdown-item" href="#">Popular</a>
<a class="dropdown-item" href="#">Trending</a>
<a class="dropdown-item" href="#">Acending</a>
<a class="dropdown-item" href="#">Decending</a>
</div>
</div>
我的 Jquery 尝试
// Find all drop downs and check their classes.
$(".dropdown-menu a").click(function() {
// Find the dropdown toggle and append the text.
$(".dropdown-toggle:first-child").append($(this).text());
});
问题:
如何在页面上没有所有下拉菜单的情况下达到我想要的结果 受到影响?此外,我还需要删除附加的文本 选择新值时。
我追求的结果;
button value = 'Sort by: Most Recent'
【问题讨论】:
标签: jquery html bootstrap-4