简体   繁体   English

如何在引导程序中选择项目时更改下拉列表的主显示

[英]How to change the main display of dropdown when an item is selected in bootstrap

This is the following http://jsfiddle.net/coderslay/enzDx/ 这是以下http://jsfiddle.net/coderslay/enzDx/

I am trying to change the text Select as soon as an item is clicked 我试图在单击项目时立即更改文本选择

So if i select One from the list then One should be displayed. 所以,如果我选择One从列表,然后One应该显示。

How to do it? 怎么做?

Working example: http://jsfiddle.net/enzDx/5/ 工作示例: http//jsfiddle.net/enzDx/5/

The only change I made to your HTML was to wrap the text "Select" with span tags with the element ID dropdown_title so it was easier to update. 我对HTML做的唯一更改是使用带有元素ID dropdown_title span标记包装文本“Select”,以便更新。

JavaScript: JavaScript的:

$('.dropdown-toggle').dropdown();
$('#divNewNotifications li').on('click', function() {
    $('#dropdown_title').html($(this).find('a').html());
});​

Basically all I'm doing is whenever the user clicks on a list item, I update the drop-down title text with the text of the link contained in the last item. 基本上我所做的只是当用户点击列表项时,我使用最后一项中包含的链接文本更新下拉标题文本。

You can check the click event on the dropdown-menu : 您可以在dropdown-menu查看click事件:

$('.dropdown-menu > li').click(function() {
    var $toggle = $(this).parent().siblings('.dropdown-toggle');
    $toggle.html("<i class=\"icon icon-envelope icon-white\"></i> " + $(this).text() + "<span class=\"caret\"></span>")
});

jsFiddle : http://jsfiddle.net/enzDx/6/ . jsFiddle: http//jsfiddle.net/enzDx/6/

HTML: HTML:

<ul class="nav nav-pills left">
    <li class="dropdown active span8">
        <a class="dropdown-toggle" id="inp_impact" data-toggle="dropdown">
            <i class="icon icon-envelope icon-white"></i>
            <span id="text">Select</span><span class="caret"></span>
        </a>
        <ul ID="divNewNotifications" class="dropdown-menu">
            <li><a>One</a></li>
            <li><a>Two</a></li>
            <li><a>Three</a></li>
        </ul>
    </li>
</ul>

JavaScript: JavaScript的:

$('.dropdown-toggle').dropdown();
$('#divNewNotifications li > a').click(function(){
    $('#text').text($(this).html());
});

DEMO DEMO

I have just added a span around the Select text in html to do the Javascript code simpler. 我刚刚在html中的Select文本中添加了一个范围,以简化Javascript代码。

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

相关问题 如何从Bootstrap下拉菜单中显示所选项目? - How to display selected item from Bootstrap dropdown? 如何使用Jquery更改Bootstrap下拉所选项目? - How to change Bootstrap dropdown selected item with Jquery? 如何在Angular中的Bootstrap下拉标题上显示所选项? - How to display the selected Item on Bootstrap Dropdown Title in Angular? 如何在Bootstrap下拉标题中显示带有图标的所选项目? - How to display selected item with icons in Bootstrap dropdown title? 如何在引导程序下拉列表的标题中显示所选项目? 以及如何在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? Bootstrap 3:选择下拉菜单项时如何生成新块? - Bootstrap 3: how to generate a new block when a dropdown item is selected? Bootstrap Dropdown选定的项目 - Bootstrap Dropdown selected item 选择一个项目时更改下拉菜单的文本 - Change the text of dropdown menu when an item is selected 如何在dropDown上显示所选列表项的文本? - How to display text of selected List item on dropDown? 为什么在WordPress的bootstrap下拉列表中不显示所选项目? - Why does not display selected item in bootstrap dropdown in WordPress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM