如何通过外部按钮单击打开引导下拉列表

时间:2015-06-25 09:06:44

标签: javascript jquery twitter-bootstrap

我读过半个互联网,但无处可寻找如何做到这一点的明确例子。一堆“解决方案”,但到目前为止没有任何作用..

这是我试过的:

$('#openner').on('click', function(e){
    e.preventDefault();
    // Nothing works bellow :(
    //$('.dropdown-toggle').trigger('click');
    //$('.dropdown-toggle').click().parent().addClass('open');
    //$('.dropdown-toggle').trigger('click.bs.dropdown'');
    $('.dropdown-toggle').click().addClass('open');
});

JSFiddle

3 个答案:

答案 0 :(得分:2)

尝试:

$('#openner').on('click', function(e){
   $('ul[aria-labelledby="dropdownMenu1"]').toggle();
});

jsfiddle:http://jsfiddle.net/mLuarcdc/4/

答案 1 :(得分:0)

OK here is my answer after all tweaking around. I realized that I can't change the class .open on the div.dropdown so I decided to use my own class, in this case .opened, and all is working.

Code:

$('#openner').on('click', function(e){
    e.preventDefault();
    // Nothing works bellow :(
    //$('.dropdown-toggle').trigger('click');
    //$('.dropdown-toggle').click().parent().addClass('open');
    //$('.dropdown-toggle').trigger('click.bs.dropdown'');

    $('.dropdown-toggle').click().parent().toggleClass('opened');
});

Here: Fiddle

答案 2 :(得分:0)

1-您可以使用jQuery执行此操作:

$('#myButton').click(function() {
    $('#myDropdown').slideToggle();
}

或者只是:

<button type="button" onclick="$('#myDropdown').slideToggle();">Click me !</button>

2-或者您可以使用Bootstrap的下拉菜单(&#39;切换&#39;)方法:

$('#myButton').click(function() {
    $('#myDropdown').dropdown('toggle');
}

或者只是:

<button type="button" onclick="$('#myDropdown').dropdown('toggle');">Click me !</button>

这将切换下拉列表的情况,这意味着如果它被打开则关闭它并在它关闭时打开它。
注意:它们都需要jQuery