5

$('.btn-group').on('focus', '.dropdown-menu li a', function() {

  $(this).parents('.dropdown-menu').find('li').removeClass('active');
  $(this).parent('li').addClass('active');


  //Displays selected text on dropdown-toggle button
  $(this).closest(":has(button)").find('button').html('<div class="dropDownSelected">' + $(this).html() + '</div>' + '<span class="caret" style="float:right;"></span>');

});
.btn-group,
.dropdown-menu {
  max-width: 150px;
}
.dropdown-menu li a {
  white-space: normal!important;
  ;
}
.dropDownSelected {
  white-space: normal;
  margin-right: 22px !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="container">
  <div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Action <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <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 which is of two lines or more</a>
      </li>
    </ul>
  </div>
  <div>
我们使用 Bootstrap 的按钮下拉组件来创建类似 SELECT TAG 的体验。用户可以单击下拉菜单并选择任何选项,相同的值会立即显示在按钮内(可通过鼠标和键盘访问)

第 1 步:单击下拉菜单中的单行选项值。

第 2 步:现在单击下拉列表中的一些多行选项值。

第 3 步:现在再次单击下拉列表中的一些单行选项值。

问题:在第 3 步之后,下拉列表(即 ul)没有消失。

预期结果:在第 3 步之后,下拉列表(即 ul)应在每次选择选项时消失(鼠标单击和键盘使用)。

请忽略插入符号和设计相关的小错误

4

2 回答 2

2

为什么不手动关闭列表?

替换这一行:

$(this).parents('.dropdown-menu').find('li').removeClass('active');

有了这个:

$(this).parents('.dropdown-menu').parent().removeClass('open').end().find('li').removeClass('active');

这将是结束代码:

$('.btn-group').on('focus', '.dropdown-menu li', function() {
    $(this).siblings('.active').removeClass('active').end().addClass('active');

   //Displays selected text on dropdown-toggle button
   $(this).closest(":has(button)").find('button').html('<div class="dropDownSelected">' + $(this).children('a').html() + '</div>' + '<span class="caret" style="float:right;"></span>');
}).on('click mouseup', '.dropdown-menu li a', function() {
   $(this).parents('.dropdown-menu').parent().removeClass('open');
});
.btn-group,
.dropdown-menu {
  max-width: 150px;
}
.dropdown-menu li a {
  white-space: normal!important;
  ;
}
.dropDownSelected {
  white-space: normal;
  margin-right: 22px !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="container">
  <div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Action <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <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 which is of two lines or more</a>
      </li>
    </ul>
  </div>
  <div>

于 2015-10-25T23:54:30.540 回答
0

只需更改 JS 以删除或隐藏 DOM。

例如:

$('.btn-group').on('focus', '.dropdown-menu li a', function() {

  $(this).parents('.dropdown-menu').find('li').removeClass('active');
  $(this).parent('li').addClass('active');


  //Displays selected text on dropdown-toggle button
  $(this).closest(":has(button)").find('button').html('<div class="dropDownSelected">' + $(this).html() + '</div>' + '<span class="caret" style="float:right;"></span>');
  
 $('.dropdown-menu').hide()


});
.btn-group,
.dropdown-menu {
  max-width: 150px;
}
.dropdown-menu li a {
  white-space: normal!important;
  ;
}
.dropDownSelected {
  white-space: normal;
  margin-right: 22px !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="container">
  <div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Action <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <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 which is of two lines or more</a>
      </li>
    </ul>
  </div>
  <div>
现在,如果您运行它,它就不再是下拉菜单了。然后只需按照您想要的方式设置您创建的按钮的样式。

于 2015-10-23T15:43:24.907 回答