显示下拉列表选定值

时间:2016-07-29 12:42:07

标签: jquery twitter-bootstrap drop-down-menu

我有一个简单的twitter bootstrap下拉菜单标记,我需要显示所选的值,但它没有发生。

<div class="container" style="padding-top: 10em;">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="dropdown">
                    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
                        Select                         <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#">APP1</a></li>
                        <li><a href="#">APP2</a></li>
                        <li><a href="#">APP3</a></li>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $('.dropdown-menu a').click(function () {
            //Need to show the selected Value
        });
    });
</script>

在上面的代码中,单击事件正在运行,但未选择并显示值。 怎么办?

2 个答案:

答案 0 :(得分:2)

而不是使用jQuery click,您可以直接使用bootstrap“click.bs.dropdown”事件。

我的片段:

$(function () {
  
  $('.dropdown').on('click.bs.dropdown', 'a', function (e) {
    $('.dropdown .btn:first-child')[0].childNodes[0].textContent = e.target.textContent + ' ';
  });

  // In order to start with an element selected you can:
  $('.dropdown').find('a:contains("APP3")').trigger('click.bs.dropdown');
  
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container" style="padding-top: 10em;">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="dropdown">
                    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
                        Select                         <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#">APP1</a></li>
                        <li><a href="#">APP2</a></li>
                        <li><a href="#">APP3</a></li>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</div>

答案 1 :(得分:1)

使用$(this).text()获取所选值文字:

&#13;
&#13;
$(document).ready(function () {
   $('.dropdown-menu a').click(function () {
      console.log($(this).text());
      var cache = $('.btn-primary').children();
      $('.btn-primary').text($(this).text()).append(cache);
   });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container" style="padding-top: 10em;">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="dropdown">
                    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
                        Select                         <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#">APP1</a></li>
                        <li><a href="#">APP2</a></li>
                        <li><a href="#">APP3</a></li>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;