使用bootstrap下拉菜单显示/隐藏容器?

时间:2018-04-02 16:33:00

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

我有下拉菜单,应显示用户从下拉菜单中选择的部分。我创建了一个简单的示例来显示我的代码以及我如何显示/隐藏容器。到目前为止一切正常。

$(document).ready(function() {
  $('#main-menu').find('a[data-container=hearing-container]').trigger('click').parent().addClass('active');
  $('#main-container > div[id=test-container]').show();
  $('#main-container > div:not([id=test-container])').hide();

});

$("#main-menu li a").on('click', mainMenu);

function mainMenu() {
  var itemID = $(this).attr('data-container');
  $(this).parent().addClass('active').siblings().removeClass('active');
  $('#main-container > div[id="' + $(this).attr('data-container') + '"]').show();
  $('#main-container > div:not([id="' + $(this).attr('data-container') + '"])').hide();
}
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div id="main-container" class="container">
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <p class="navbar-text"><span class="glyphicon glyphicon-home"></span> <b>Welcome to Single Page Application!</b></p>
      <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu<span class="caret"></span></a>
          <ul id="main-menu" class="dropdown-menu">
            <li><a href="#" data-container="home-container">Home</a></li>
            <li><a href="#" data-container="test-container">Test</a></li>
            <li><a href="#" data-container="reports-container">Reports</a></li>
            <li><a href="#" data-container="settings-container">Settings</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>
  <div id="home-container">
    <ul id="agecny-navbar" class="nav nav-tabs">
      <li class="active"><a data-toggle="tab" href="#agency-myaccount">My Account</a></li>
      <li><a data-toggle="tab" href="#agency-finduser">Find User</a></li>
    </ul>
    <div class="tab-content">
      <div id="agency-myaccount" class="tab-pane fade in active" data-name="MyAccount">
        <form name="frm_myaccount" id="frm_myaccount" class="frm-agencySubmit" autocomplete="off">
          <div class="form-group required">
            <label class="control-label" for="acctive">Account Active</label>
            <select class="form-control" name="frm_active" id="frm_active" required>
                                        <option value="">-- Select the option --</option>
                                        <option value="0">No</option>
                                        <option value="1">Yes</option>
                                    </select>
          </div>
      </div>
    </div>
  </div>
</div>
<div id="test-container">
  Test
</div>
<div id="reports-container">
  Reports
</div>
<div id="settings-container">
  Settings
</div>
</div>

然后我添加了更多在我的容器中实际使用的代码。在这种情况下,前一个示例中的代码会中断并显示所有容器。我已经一步一步走了,似乎一旦我添加form标签,元素代码开始破坏。如果我删除一切正常。我不确定bootstrap是否不支持任何这些方法或其他导致此问题的东西。如果有人在我的代码中看到问题出在哪里,请告诉我。谢谢。

2 个答案:

答案 0 :(得分:1)

没有form结束标记,更改它。希望在你关闭</form>之后它能正常工作。

答案 1 :(得分:1)

错过&#34;表格&#34;关闭并添加一个额外的&#34; Div&#34;在div之后关闭(&#34;#home-container&#34;)。

更新以下代码:

<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div id="main-container" class="container">
            <nav class="navbar navbar-default">
        <div class="container-fluid">
          <p class="navbar-text"><span class="glyphicon glyphicon-home"></span> <b>Welcome to Single Page Application!</b></p>
          <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
              <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu<span class="caret"></span></a>
              <ul id="main-menu" class="dropdown-menu">
                <li><a href="#" data-container="home-container">Home</a></li>
                <li><a href="#" data-container="test-container">Test</a></li>
                <li><a href="#" data-container="reports-container">Reports</a></li>
                <li><a href="#" data-container="settings-container">Settings</a></li>
              </ul>
            </li>
          </ul>
        </div>
      </nav>
            <div id="home-container">
                <ul id="agecny-navbar" class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#agency-myaccount">My Account</a></li>
                    <li><a data-toggle="tab" href="#agency-finduser">Find User</a></li>
                </ul>
                <div class="tab-content">
                    <div id="agency-myaccount" class="tab-pane fade in active" data-name="MyAccount">
                        <form name="frm_myaccount" id="frm_myaccount" class="frm-agencySubmit" autocomplete="off">
                            <div class="form-group required">
                                <label class="control-label" for="acctive">Account Active</label>
                                <select class="form-control" name="frm_active" id="frm_active" required>
                                    <option value="">-- Select the option --</option>
                                    <option value="0">No</option>
                                    <option value="1">Yes</option>
                                </select>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div id="test-container">
                Test
            </div>
            <div id="reports-container">
                Reports
            </div>
            <div id="settings-container">
                Settings
            </div>
        </div>