1

我已经研究过这个问题,但当我单击由于 boostrap-dropdown.js 而关闭的表单时,我仍然无法让它工作。但是,我正在按照本指南使用 bootstrap: Guide在我的导航栏上创建登录下拉菜单,并且它们包含 JS 以阻止表单关闭,但它对我不起作用。

这是我的代码:

<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container"><!-- Collapsable nav bar -->
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>

<!-- Your site name for the upper left corner of the site -->
<a class="brand"><img name="" src="bglogo.png"  alt=""></a>

<!-- Start of the nav bar content -->
<div class="nav-collapse"><!-- Other nav bar content -->

<!-- The drop down menu -->
<ul class="nav pull-right">
<li><a href="/users/sign_up">Sign Up</a></li>
<li class="divider-vertical"></li>
<li class="drop down">
<a class="dropdown-toggle" href="#" data-toggle="dropdown">Sign In <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
<!-- Login form here -->
<form action="Checklogin.php" method="post" accept-charset="UTF-8">
<input id="user_username" style="margin-bottom: 15px;" type="text" name="user[username]" size="30" />
<input id="user_password" style="margin-bottom: 15px;" type="password" name="user[password]" size="30" />
<input id="user_remember_me" style="float: left; margin-right: 10px;" type="checkbox" name="user[remember_me]" value="1" />
<label class="string optional" for="user_remember_me"> Remember me</label>

<input class="btn btn-primary" style="clear: left; width: 100%; height: 32px; font-size:    13px;" type="submit" name="commit" value="Sign In" />
</form>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>

以及我在关闭正文标记之前插入的 Javascript:

<!-- Placed at the end of the document so the pages load faster -->

<script src="twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>

<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-transition.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-alert.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-modal.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-dropdown.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-scrollspy.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-tab.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-tooltip.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-popover.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-button.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-collapse.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script>
<script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-typeahead.js"></script>
<script>
$(function() {
// Setup drop down menu
$('.dropdown-toggle').dropdown();

// Fix input element click problem
$('.dropdown input, .dropdown label').click(function(e) {
e.stopPropagation();
});
});</script>

谢谢!

编辑:我想得越多,我就越意识到它必须是一个 Javscript 问题,因为表单和导航栏都很好,但是当我“单击”下拉菜单时它会关闭。

4

1 回答 1

1

您在 js 中引用了错误的容器.dropdown,它应该是.dropdown-menu; 试试这个:

JS

$('.dropdown-menu input, .dropdown-menu label').click(function(e) {
    e.stopPropagation();
});

快速提示:您可以通过使用bootstrap.js脚本包而不是一个一个地加载它们来最小化页面中 js 调用的数量。

于 2012-07-10T02:17:56.717 回答