如何确保当我选择下拉菜单中的某个元素时,它会关闭,然后我可以重新打开它?
这是我的代码,如果我选择一个项目菜单关闭但我不能重新打开它。
$(document).ready(function(){
$("a.sorting").on('click', function(e) {
$("#sort").dropdown("toggle");
e.stopPropagation();
});
});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul class="nav navbar-nav">
<li class="dropdown" id="sort">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Ordina per
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="sorting" value="Premio">Premio</a></li>
<li><a class="sorting" value="Recenti">Recenti</a></li>
<li><a class="sorting" value="Deadline">Deadline</a></li>
<li><a class="sorting" value="Numero Partecipanti">Numero Partecipanti</a></li>
</ul>
</li>
</ul>
&#13;
答案 0 :(得分:0)
您不需要处理事件onclick事件。只需删除你的jquery代码即可。
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul class="nav navbar-nav">
<li class="dropdown" id="sort">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Ordina per
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a class="sorting" value="Premio">Premio</a></li>
<li><a class="sorting" value="Recenti">Recenti</a></li>
<li><a class="sorting" value="Deadline">Deadline</a></li>
<li><a class="sorting" value="Numero Partecipanti">Numero Partecipanti</a></li>
</ul>
</li>
</ul>
&#13;