jquery - 关闭 bootstrap-select/select2 的自动对焦

标签 jquery twitter-bootstrap jquery-select2 bootstrap-select

http://www.bootply.com/YAQrE52K6X

    $("#dataCombo").selectpicker({
    multiple:true
    });

    <div class="container">

  <h3>Multiple Select</h3>
  <select id="dataCombo" class="selectpicker" data-live-search="true" multiple="multiple" style="width:400px;">

    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
    <option value="four">Four</option>
    <option value="five">Five</option>
    <option value="six">Six</option>
  </select>

</div>

我想关闭自动对焦,这样输入内容就不会要求我在单击下拉菜单后立即进行搜索。尝试使用 .blur 等...但到目前为止还没有。环顾四周寻找解决方案。由于同样的问题,改为使用“select2”中的“bootstrap-select”。设备上的键盘显示覆盖多个下拉项

最佳答案

我无法使用 bootstrap-select 修复此问题(似乎他们在触发 show.bs.selectshown.bs.select 时遇到一些问题事件),但由于您还提到您使用 select2 尝试过此操作,因此这里是 select2 的解决方案:

  $("#dataCombo").select2({
    closeOnSelect: false
  });
  $("#dataCombo").on('select2:open', function () {
    $(document.activeElement).blur()
  });

检查这个例子:
https://jsfiddle.net/yw61h28z/

更新 - 2016/12/05

设法让它工作(花了一些时间来处理焦点/模糊/目标的事情)。

$(document).ready(function() {
  $("#dataCombo").select2({
    closeOnSelect: false
  });
  $("#dataCombo").one('select2:open', function (e) {
    $(document.activeElement).blur()
  });
  $("#dataCombo").on('select2:close', function(e) {
    if ($(event.target).parents('.select2').length) {
      // The closing was done inside the select2 container
    } else {
      $("#dataCombo").one('select2:open', function (e) {
        $(document.activeElement).blur()
      });
    }
  });
});

这是我原来的 jsfiddle 的更新:
https://jsfiddle.net/yw61h28z/4/

Here is the idea behind the code:
1. The first time the select2 is opened - blur the input (but only one time) - this makes sure the select2 menu is opened, but the input isn't focused (so the keyboard is not displayed).
2. On closing the select2 menu - check the element that caused the close.
2.1. If that element is part of the select2 block - ignore it and close keep closing the menu.
2.2 Else - The next time the select2 menu is opened we blur the input (again - only 1 time).

关于jquery - 关闭 bootstrap-select/select2 的自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40509189/

相关文章:

响应式背景图片上的 html 文本

php - JQuery Select2 - 从 PHP/MySQL 结果中格式化嵌套列表

javascript - 为什么我的鼠标悬停层没有显示在其他浏览器上?

javascript - 在日期时间选择器 Bootstrap 中设置一天的时间范围

javascript - 使用 FetchContent JS 发出警报

javascript - Bootstrap 折叠小部件不起作用

javascript - 无法使用 JQuery 设置 Select2 下拉值

javascript - 通过 javascript/jquery 关闭 select2 下拉菜单

php - Value == DisplayText in Field Options 否则字段为空白

javascript - 使用 jQuery 将 Javascript 代码插入 DIV