jQuery事件处理程序不会在Bootstrap下拉开关上连续单击时触发

时间:2018-09-04 16:50:12

标签: javascript jquery html twitter-bootstrap

无论何时用户打开下拉列表,我都应该从数据库中填充它:

为此,我想为打开下拉菜单的按钮注册一个事件处理程序:

$(document).on('click', '.schedulesDropdown', function() {
  alert("call api with ajax and populate dropdown with result")
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<button id="{{id}}" class="btn btn-primary dropdown-toggle schedulesDropdown" data-toggle="dropdown"></button>
<div class="dropdown-menu">
  stuff
</div>

问题在于它仅触发一次。

如果我删除引导程序,它会像一种魅力:

$(document).on('click', '.schedulesDropdown', function() {
  alert("call api with ajax and populate dropdown with result")
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

<button id="{{id}}" class="btn btn-primary dropdown-toggle schedulesDropdown" data-toggle="dropdown"></button>
<div class="dropdown-menu">
  stuff
</div>

由于问题出在引导程序上,我不知道下一步是什么。

1 个答案:

答案 0 :(得分:0)

添加onclick事件,而不是通过DOM访问它,并将 this 关键字作为参数传递给event中调用的方法。 <button onclick=“return methodName(this)” >