简体   繁体   English

如何在引导程序下拉列表的标题中显示所选项目? 以及如何在JavaScript警报框中显示所选项目?

[英]How to display selected item in the title of the bootstrap dropdown list ? and how to display selected item on a javascript alert box?

I have tried lots of examples given in this type of questions but still can't find how to display selected item on the title of the bootstrap dropdown list and how to get the selected item through javascript. 我已经尝试了许多此类问题的示例,但仍然找不到如何在引导下拉列表的标题上显示所选项目以及如何通过javascript获取所选项目的方法。 if someone know the code please help me with the links that I should include in the head section. 如果有人知道代码,请帮助我提供我应该放在头部的链接。 Because I think it may be I missing something in the head section. 因为我认为可能是我的头部缺少了一些内容。 here is sample dropdown list. 这是示例下拉列表。

<div class="btn-group">
   <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
   Option: <span class="selection">Option 1</span><span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li><a href="#">Option 1</a></li>
        <li><a href="#">Option 2</a></li>
        <li><a href="#">Option 3</a></li>
   </ul>
</div>

I am modified HTML little bit, and added jquery event: 我对HTML进行了一点修改,并添加了jquery事件:

<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Dropdown:  <span class="selected"></span>
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
    <li><a href="#">Action</a></li>
    <li><a href="#">Another action</a></li>
    <li><a href="#">Something else here</a></li>
    <li><a href="#">Separated link</a></li>
  </ul>
</div>

Jquery: jQuery的:

$('div.dropdown ul.dropdown-menu li a').click(function (e) {
    $('.selected').html($(this).html());
})

--DEMO-- --DEMO--

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

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