将select2与jQuery虚拟键盘配合使用
Using select2 with a jQuery Virtual Keyboard?
我想知道是否有任何方法可以使用jQuery虚拟键盘在Select2选择栏中输入。
因为当您在Select2组件之外单击时,下拉列表会自动关闭。这不允许你使用任何基于网络的虚拟键盘。
如果你有想法,谢谢你的帮助!
Phew,花了一段时间!我为你做了一个简单的演示。只需点击选择,就会弹出一个键盘。
这里的技巧是,在用户单击select之前,select2搜索输入不存在于DOM中,因此我们使用select2:open
事件在select2文本输入附加到DOM后找到它,然后将jquery虚拟键盘附加到它上。
// Make the Select2 select
$('#sel').select2({
dropdownCssClass: 'keyboard_target_wrapper'
});
// We don't want to accidentally make multiple virtual keyboards
let kb_bound = false;
// Wait until the Select2 select is opened
// as the Select2 select input element
// might not be in the DOM yet
$('#sel').on('select2:open', function (e) {
// Save the search input query
let $select2SearchInput = $('.keyboard_target_wrapper')
.find('.select2-search__field')
// If we already have an initialized virtual keyboard
// we just return it
if(kb_bound) {
$('.keyboard_target_wrapper')
.find('.select2-search__field')
.getkeyboard();
return;
}
// Create the virtual keyboard
$select2SearchInput.keyboard({
openOn: 'focus',
appendLocally: false, // Append to body
appendTo: 'body',
usePreview: false, // Hide the text preview on the keyboard
reposition: true, // Make keyboard react to viewport changes
layout: 'qwerty',
css: {
container: 'ui-widget-content ui-widget ui-corner-all ui-helper-clearfix',
popup: ''
},
stopAtEnd: true,
resetDefault: false,
beforeInsert: function(e, keyboard, el, str) {
// Must trigger keyup event on the Select2
// search input manually, because
// the virtual keyboard doesn't do it
$select2SearchInput.keyup();
// Just return the input text normally
return str;
},
});
kb_bound = true;
});
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/ui-lightness/jquery-ui.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/virtual-keyboard/1.30.4/css/keyboard.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="https://code.jquery.com/ui/1.13.1/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/virtual-keyboard/1.30.4/js/jquery.keyboard.min.js"></script>
<select id="sel">
<option value="Cat">Cat</option>
<option value="Dog">Dog</option>
<option value="Bird">Bird</option>
<option value="Fish">Fish</option>
<option value="Dinosaur">Dinosaur</option>
</select>
相关文章:
- javascript虚拟键盘
- iPad虚拟键盘-哪一个-javasctript解决方案
- 将select2与jQuery虚拟键盘配合使用
- JavaScript 中的虚拟键盘
- 虚拟键盘事件
- 虚拟键盘在更新到 Chrome v50 后不再工作
- 在Windows 8 Metro模式下,在浏览器中获取虚拟键盘的高度
- Javascript中的Mac/Windows弹出式虚拟键盘
- 虚拟键盘jquery在引导模式中显示错误
- 虚拟键盘出现问题
- 如果处理字符串值的文本区域是只读的,如何使用 JavaScript 在虚拟键盘上使用 CAPSLOCK 功能
- 一些功能可以确定三星八达上虚拟键盘的打开
- 如何使用虚拟键盘在 Opera Mobile 中为简单表单触发 onsubmit() 事件
- iPhone:点击/单击“开始”按钮后,虚拟键盘不会隐藏
- 打开虚拟键盘时,我的应用程序网页视图不会缩小
- 启用/禁用多个谷歌虚拟键盘
- 在iOS8中,使用.focus()将显示虚拟键盘并在触摸后滚动页面
- jQuery虚拟键盘插件在Windows8触摸设备上两次插入相同的字符
- 虚拟键盘/滚动奇怪事件
- Ipad上的虚拟键盘使输入[类型=范围]滑块拇指消失