jQuery / Bootstrap:下拉切换未将所选菜单项设置为默认选定项

时间:2016-05-31 18:10:51

标签: javascript jquery html twitter-bootstrap twitter-bootstrap-3

我有一个引导下拉菜单,但我无法将所选菜单项设置为默认选定的下拉项目?

<div class="dropdown">
   <button class="btn dropdown-toggle" type="button" id="drpDownCat"
    data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
     Select a Category<span class="caret"></span>
   </button>

    <ul class="dropdown-menu" aria-labelledby="drpDownCat">
       <li><a class="dropdown-item" href="#">Category 1</a></li>
       <li><a class="dropdown-item" href="#">Category 2</a></li>
       <li><a class="dropdown-item" href="#">Category 3</a></li>
       <li><a class="dropdown-item" href="#">Category 4</a></li>
   </ul>
</div>

   <script>
   $('.dropdown-toggle').click(function()
     {
       $('.dropdown-menu').toggle();
     });

   </script>

我为上面的代码创建了JSFiddle

2 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="dropdown">
    <button class="btn dropdown-toggle" type="button" id="drpDownCat"
    data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Select a Category<span class="caret"></span>
  </button>

  <ul class="dropdown-menu" aria-labelledby="drpDownCat">
    <li><a class="dropdown-item" href="#">Category 1</a></li>
    <li><a class="dropdown-item" href="#">Category 2</a></li>
    <li><a class="dropdown-item" href="#">Category 3</a></li>
    <li><a class="dropdown-item" href="#">Category 4</a></li>
  </ul>
</div>
<script type="text/javascript">
$('.dropdown-item').click(function(){
  $("#drpDownCat").text(this.text)
});
</script>
</body>
</html>

答案 1 :(得分:1)

试试这个

$(function() {
  $(".dropdown-menu li a").click(function() {
    $(".btn:first-child").text($(this).text());
    $(".btn:first-child").val($(this).text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="dropdown">
  <button class="btn dropdown-toggle" type="button" id="drpDownCat" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Select a Category<span class="caret"></span>
  </button>

  <ul class="dropdown-menu" aria-labelledby="drpDownCat">
    <li><a class="dropdown-item" href="#">Category 1</a>
    </li>
    <li><a class="dropdown-item" href="#">Category 2</a>
    </li>
    <li><a class="dropdown-item" href="#">Category 3</a>
    </li>
    <li><a class="dropdown-item" href="#">Category 4</a>
    </li>
  </ul>
</div>