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.select
和 shown.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 theinput
(but only one time) - this makes sure the select2 menu is opened, but theinput
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 theselect2
block - ignore it and close keep closing the menu.
2.2 Else - The next time the select2 menu is opened weblur
theinput
(again - only 1 time).
关于jquery - 关闭 bootstrap-select/select2 的自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40509189/