jQuery Twitter Bootstrap:如何获取下拉列表的选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19736008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twitter Bootstrap: How do I get the selected value of dropdown?
提问by Matthew David Jankowski
I need to get the value a user selects from a twitter bootstrap dropdown. Once the user selects a value I want the dropdown to display that value. So, the user selects 'pending' then I want to change the selected dropdown value from that and, when the user completes the form, I want to retrieve the selected value. Would someone be able to point me in the right direction?
我需要获取用户从 twitter bootstrap 下拉列表中选择的值。一旦用户选择了一个值,我希望下拉列表显示该值。因此,用户选择“待定”,然后我想从中更改选定的下拉值,当用户完成表单时,我想检索选定的值。有人能指出我正确的方向吗?
<div class="btn-group">
<i class="dropdown-arrow dropdown-arrow-inverse"></i>
<button class="btn btn-primary">status</button>
<button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-inverse">
<li><a href="#fakelink">pending</a></li>
<li><a href="#fakelink">active</a></li>
<li><a href="#fakelink">discontinued</a></li>
</ul>
</div>
回答by PSL
Bootstrap provides you with minimum required functionality for your implementation, they aren't dropdowns instead they are mere ul, li's with css applied on it and some events registered in the bootstrap plugin for toggling the button to display them as dropdown. This one you need to do on your own.
Bootstrap 为您提供了实现所需的最低限度的功能,它们不是下拉菜单,而只是 ul、li 和 css 应用在其上,以及在引导插件中注册的一些事件,用于切换按钮以将它们显示为下拉菜单。这个你需要自己做。
Set up some identifier to the status button say a className btnStatus
and bind a click event to the dropdown links. and do something like this?
为状态按钮设置一些标识符,例如 classNamebtnStatus
并将单击事件绑定到下拉链接。并做这样的事情?
$('.dropdown-inverse li > a').click(function(e){
$('.btnStatus').text(this.innerHTML);
});
回答by BandfuzzAlex
I had the same issue. The solution is pretty easy and I think the answers above may be overcomplicating a bit.
我遇到过同样的问题。解决方案非常简单,我认为上面的答案可能有点过于复杂。
Just use $(this).text()
只需使用 $(this).text()
Assign a class name to the "" bit then..
为“”位分配一个类名,然后..
$(".yourUlClassName li a").click( function() {
var yourText = $(this).text();
});
回答by Mohsen Rasouli
you can try this:
你可以试试这个:
u can use class or id for selector to select easier the element.
for use the value also u can use the value
in u javascript with jQuery.
您可以使用 class 或 id 作为选择器来更轻松地选择元素。为了使用该值,您也可以value
在 u javascript 中使用 jQuery。
$().ready(function(){
$('a').click(function(){
var value = $(this).text();
alert(value);
});
});