[英]Use Twitter Bootstrap Dropdown toggle to trigger another dropdown toggle or div
I need to make a dropdown toggle with some options, and when the user selects one of those options from dropdown, it either triggers another dropdown toggle to appear right underneath OR Call a Div and display some content. 我需要创建一个带有某些选项的下拉菜单,当用户从下拉菜单中选择其中一个选项时,它会触发另一个下拉菜单以显示在其正下方或调用Div并显示一些内容。
I have looked at the BS documentation and they don't really go into the triggers or what's needed to call another div or dropdown to display. 我查看了BS文档,但它们并没有真正涉及触发器或调用另一个div或下拉菜单进行显示所需要的内容。
Here's a link to jsfiddle on what I have. 这是我所拥有的jsfiddle的链接 。
I want the second dropdown to only appear when user selects "Option 1" from previous dropdown toggle. 我希望第二个下拉菜单仅在用户从上一个下拉菜单中选择“选项1”时显示。
<div class="question">
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Select one <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#" id="opt1" >Option 1</a></li>
<li><a href="#" id="opt2">Option 2</a></li>
<li><a href="#" id="opt3">Option 3</a></li>
<li><a href="#" id="opt3">Option 4</a></li>
</ul>
</div>
<div class="question">
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Another One <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#" id="opt5" >Option 5</a></li>
<li><a href="#" id="opt6">Option 6</a></li>
<li><a href="#" id="opt7">Option 7</a></li>
<li><a href="#" id="opt8">Option 8</a></li>
</ul>
</div>
</div>
Would really appreciate any help I can get. 非常感谢我能提供的任何帮助。
You have multiple options to do it. 您有多种选择可做。
$('.question .visitor li:first-child a').click(function () { // First li of it's parent
$('.opt1').fadeToggle();
});
OR 要么
$('.question .visitor li:eq(0) a').click(function () { // 0 is the first of all relevant elements criteria
$('.opt1').fadeToggle();
});
OR 要么
$('#opt1').click(function () { // Select by ID
$('.opt1').fadeToggle();
});
First off, you'll need to detect a click event on the dropdown box in question, on an <li>
if you want that to be the trigger. 首先,如果您希望触发触发,则需要在相关的下拉框上的
<li>
上检测到单击事件。 So: 所以:
$( "ul.dropdown-menu li" ).click(function() {
});
Now, whenever an <li>
is clicked inside the dropdown-menu, that callback function will be called. 现在,每当在下拉菜单中单击
<li>
,都会调用该回调函数。 Here, if you need another dropdown or a div to appear, use the appendChild()
method to add those items where needed. 在这里,如果您需要另一个下拉菜单或div出现,请使用
appendChild()
方法在需要的地方添加这些项目。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.