简体   繁体   English

jQuery获取单击的文本下拉菜单引导

[英]Jquery get clicked text dropdown menu bootstrap

I am using Bootstrap and i have the following dropdown menu : 我正在使用Bootstrap,并且具有以下下拉菜单:

<div class="btn-group">
        <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
                 number of rooms
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
                <li>
                    <a href="#" >1</a>
                </li>
                <li>
                    <a href="#" >2</a>
                </li>
                <li>
                    <a href="#" >3</a>
                </li>
                <li>
                    <a href="#" >4</a>
                </li>
                <li>
                    <a href="#" >5</a>
                    </li>               
            </ul>
        </div>

I want to get the selected text when i click on an entry of the drop down menu. 当我单击下拉菜单条目时,我想获取所选文本。 I've tried : 我试过了 :

$('.dropdown-menu :selected').text();
$('.dropdown-menu option:selected').text();
$('.dropdown-menu').find('option:selected').text();
$('.dropdown-menu li a').text(); //returns 12345
$('.dropdown-menu > .active').text();
...

but it doesn't work. 但这不起作用。 What am i doing wrong ? 我究竟做错了什么 ? Thx 谢谢

EDIT In fact i am totally new to Javascript (i'm a java developper). 编辑实际上,我对Java完全陌生(我是Java开发人员)。 I am using Backbone and would like to have something like that : 我正在使用Backbone,并且想要这样的东西:

window.RoomView = Backbone.View.extend({

events : {
    "click .dropdown-menu"       : "test"
}

test : function(){
//display selected text
}

It depends on whether you mean you want to get the text within an existing function, or just at all: 这取决于您是要在现有函数中获取文本,还是要获取文本:

$('.dropdown-menu a').click( function () {
    var text = $(this).text();
});

I'm not a backbone expert, but perhaps... 我不是骨干专家,但也许...

events : {
    "click .dropdown-menu a"       : "test"
}

test : function(){
    //display selected text
    var text = $(this).text();
}

Just do that. 就是那样

$('.dropdown-menu li a').click(function() {
    alert($(this).text());
});
"click .dropdown-menu a"    : "displayRoomConfigForms"

displayRoomConfigForms: function(event){
  var text = $(event.currentTarget).text(); 
}

made it 做好了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM