0

我正在使用 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>

当我单击下拉菜单的条目时,我想获取选定的文本。我试过了 :

$('.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();
...

但它不起作用。我究竟做错了什么 ?谢谢

编辑事实上,我对 Javascript 完全陌生(我是一名 Java 开发人员)。我正在使用 Backbone 并希望拥有类似的东西:

window.RoomView = Backbone.View.extend({

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

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

3 回答 3

7

这取决于您的意思是要在现有函数中获取文本,还是根本不需要:

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

我不是骨干专家,但也许...

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

test : function(){
    //display selected text
    var text = $(this).text();
}
于 2013-02-22T17:55:07.173 回答
2
"click .dropdown-menu a"    : "displayRoomConfigForms"

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

做好了

于 2013-02-26T14:16:11.953 回答
2

就这样做吧。

$('.dropdown-menu li a').click(function() {
    alert($(this).text());
});
于 2013-02-22T17:55:54.997 回答