esc键关闭引导模式,但没有't关闭其中的select2下拉列表

esc key close bootstrap modal but doesn't close select2 dropdown inside it

本文关键字:下拉列表 select2 模式 esc      更新时间:2024-01-29

我在位于引导模式窗口内的下拉列表中实现了select2。

<div class="modal-body">
...
<div class="controls">
    @Html.DropDownList("Experts", new SelectList(Model.ExpertsInfo, "UserId", "FullName"),
      string.Empty, new { @class = "select", id = "expert-select", autocomplete = "true", autofocus = "", style = "width: 295px;" }                       
                </div>
...
    </div>
 $("#experts").select2({
            allowClear: true,
            minimumResultsForSearch: -1,
            formatNoMatches: function () {
                return "@CommonResources.NoMatches";
            }
        });

所有工作都很好,但如果我关闭模式窗口,当下拉列表打开时,模式窗口隐藏,但下拉列表仍然存在!我哪里搞错了?我必须为esc密钥实现我自己的处理程序吗?

选择2 API

要关闭select2,请使用以下选项:

 $('#experts').select2("close");

然后,您将根据引导程序的版本进入关闭事件:

引导3

$('#myModal').on('hidden.bs.modal', function () {
  $('#experts').select2("close");
})

引导程序2

$('#myModal').on('hidden', function () {
   $('#experts').select2("close");
})

最终解决方案

根据Anton的评论:

$('#expert-modal').keyup(function (e) { if (e.keyCode == 27) {
       $('#s2id_experts').select2("close"); 
    } 
});