最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change the main display of dropdown when an item is selected in bootstrap - Stack Overflow

programmeradmin5浏览0评论

This is the following /

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.

How to do it?

This is the following http://jsfiddle/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.

How to do it?

Share Improve this question asked Dec 28, 2012 at 8:18 coderslaycoderslay 14.4k32 gold badges81 silver badges122 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Working example: http://jsfiddle/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.

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 > 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/enzDx/6/ .

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:

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

DEMO

I have just added a span around the Select text in html to do the Javascript code simpler.

发布评论

评论列表(0)

  1. 暂无评论