Make bootstrap drop down buttons cascading
我在下面的网页上添加了两个下拉框,但我想让它们级联,然后将下拉按钮的值传递给函数。
<div class="btn-group">
<button data-toggle="dropdown" class="btn btn-success dropdown-toggle">Success <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" onClick="return false;">Action</a></li>
<li><a href="#" onClick="return false;">Another action</a></li>
<li><a href="#" onClick="return false;">Something else here</a></li>
<li class="divider"></li>
<li><a href="#" onClick="return false;">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
<div class="btn-group">
<button data-toggle="dropdown" class="btn btn-info dropdown-toggle">Info <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" onClick="return false;">Action</a></li>
<li><a href="#" onClick="return false;">Another action</a></li>
<li><a href="#" onClick="return false;">Something else here</a></li>
<li class="divider"></li>
<li><a href="#" onClick="return false;">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
As KyleT mentioned in the comments you could accomplish with enum in place of arrays. However as a comfort pick I'll be using an array. Fiddle
var states = [
"Kansas",
"Oklahoma",
"Texas"
];
$('.dropdown-menu > li > a').click(function() {
yourFunction(this.text);
});
function yourFunction(val) {
if ( val === "Action" ) {
$('#states').html('');
for ( var i in states ) {
$('#states').append("<li><a href='#'>" + states[i] + "</a></li>");
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="btn-group">
<button data-toggle="dropdown" class="btn btn-success dropdown-toggle">Success <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Action</a>
</li>
<li><a href="#">Another action</a>
</li>
<li><a href="#">Something else here</a>
</li>
<li class="divider"></li>
<li><a href="#">Separated link</a>
</li>
</ul>
</div>
<!-- /btn-group -->
<div class="btn-group">
<button data-toggle="dropdown" class="btn btn-info dropdown-toggle">Info <span class="caret"></span>
</button>
<ul class="dropdown-menu" id="states">
<li><a href="#">Action</a>
</li>
<li><a href="#">Another action</a>
</li>
<li><a href="#">Something else here</a>
</li>
<li class="divider"></li>
<li><a href="#">Separated link</a>
</li>
</ul>
</div>
<!-- /btn-group -->
链接地址: http://www.djcxy.com/p/91768.html
上一篇: 用于将字符串限制为多个离散值的JavaScript惯用法
下一篇: 使引导程序下拉按钮级联