在2下拉菜单中更改文本

时间:2014-10-11 00:30:44

标签: jquery twitter-bootstrap

我可以在下面运行此代码,但问题是当我点击消息>收件箱(或其他子项)管理员上的文本会更改两个下拉列表。

这是我的代码:

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

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="https://cdn6.f-cdn.com/js/jquery-1.7.1.min.js?v=69e9edb32340c72a95b174dee04a036e&amp;m=2" type="text/javascript"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  
  
<style type="text/css">
	.bs-example{
    	margin: 20px;
    }
</style>
</head>
<body>
<div class="bs-example">
    <nav id="myNavbar" class="navbar navbar-default" role="navigation">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Profile</a></li>
                    <li class="dropdown">
                        <a href="#" data-toggle="dropdown" class="dropdown-toggle">Messages <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a href="#" data-value="Inbox">Inbox</a></li>
                            <li><a href="#" data-value="Drafts">Drafts</a></li>
                            <li><a href="#" data-value="Sent Items">Sent Items</a></li>
                            <li class="divider"></li>
                            <li><a href="#" data-value="Trash">Trash</a></li>
                        </ul>
                    </li>
                </ul>
				<script>
				$(".dropdown-menu li a").click(function(){
  $(this).parents(".collapse").find('.dropdown-toggle').text($(this).text());
  $(this).parents(".collapse").find('.dropdown-toggle').val($(this).data('value'));
});
</script>
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a href="#" data-toggle="dropdown" class="dropdown-toggle">Admin <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li class="divider"></li>
                            <li><a href="#">Settings</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div>
    </nav>
</div>
</body>
</html>         

2 个答案:

答案 0 :(得分:0)

因为元素具有折叠类,所以你的下拉切换元素都是子元素。你应该寻找最近的父母有一个班级下拉列表。

试试这个:

<script>
  $(".dropdown-menu li a").click(function(){
  $(this).closest(".dropdown").find('.dropdown-toggle').text($(this).text());
  $(this).closest(".dropdown").find('.dropdown-toggle').val($(this).data('value'));
});
</script>

答案 1 :(得分:0)

只是为了说明你的问题:

导航到.collapse课程时,您跳转到.dropdowns的父级。

<div class="collapse navbar-collapse" >

    <ul class="nav navbar-nav">
        <li class="dropdown">
            <a class="dropdown-toggle"></a>
            <ul class="dropdown-menu">
                <li><a href="#">Inbox</a></li>
            </ul>
        </li>
    </ul>

    <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
            <a class="dropdown-toggle"></a>
            <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
            </ul>
        </li>
    </ul>
</div>

就Javascript而言,我使用closest,因此您可以在找到第一个实例后停止搜索,然后将DOM元素保存在搜索中,这样您就不必执行查找两次:

$(".dropdown-menu li a").click(function() {
  var $toggle = $(this).closest(".dropdown").find('.dropdown-toggle');
  var value = $(this).data('value')
  var text = $(this).text()

  $toggle.val(value);
  $toggle.text(text);
});

&#13;
&#13;
$(".dropdown-menu li a").click(function() {
  var $toggle = $(this).closest(".dropdown").find('.dropdown-toggle');
  var value = $(this).data('value')
  var text = $(this).text()
  
  $toggle.val(value);
  $toggle.text(text);
});
&#13;
.bs-example{
  margin: 20px;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">

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


<div class="bs-example">
  <nav id="myNavbar" class="navbar navbar-default" role="navigation">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" 
                data-toggle="collapse" 
                data-target="#bs-example-navbar-collapse-1">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">Brand</a>
      </div>
      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" 
           id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li><a href="#">Home</a></li>
          <li><a href="#">Profile</a></li>
          <li class="dropdown">
            <a href="#" data-toggle="dropdown" class="dropdown-toggle">
              Messages <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><a href="#" data-value="Inbox">Inbox</a></li>
              <li><a href="#" data-value="Drafts">Drafts</a></li>
              <li><a href="#" data-value="Sent Items">Sent Items</a></li>
              <li class="divider"></li>
              <li><a href="#" data-value="Trash">Trash</a></li>
            </ul>
          </li>
        </ul>

        <ul class="nav navbar-nav navbar-right">
          <li class="dropdown">
            <a href="#" data-toggle="dropdown" class="dropdown-toggle">
              Admin <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li class="divider"></li>
              <li><a href="#">Settings</a></li>
            </ul>
          </li>
        </ul>
      </div><!-- /.navbar-collapse -->
    </div>
  </nav>
</div>
&#13;
&#13;
&#13;