如何防止jQueryUI Autocomplete的默认键盘交互? [英] How to prevent jQueryUI Autocomplete's default keyboard interactions?
问题描述
我特别想在按下ENTER键时阻止交互,以防止jQueryUI Autocomplete选择当前关注的项目并关闭菜单.
I'd specifically like to prevent the interaction when I push ENTER to prevent jQueryUI Autocomplete from selecting the currently focused item and closing the menu.
我指的是以下文档: http://api.jqueryui.com /autocomplete/#event-change
I'm referring to the following documentation: http://api.jqueryui.com/autocomplete/#event-change
推荐答案
更新:
原理是一样的.只需更改setTimeout
函数来提交表单即可(如此处所示: http://jsfiddle.net/X8Ghc /8/)
The principle is the same. Just change the setTimeout
function to submit your form instead (as demonstrated here: http://jsfiddle.net/X8Ghc/8/)
更新:
您是正确的,$(this)
应该是指ui-menu
(显然没有).新的小提琴 http://jsfiddle.net/X8Ghc/7/运作良好.
You are correct, $(this)
was supposed to refer to the ui-menu
(and obviously didn't). The new fiddle, http://jsfiddle.net/X8Ghc/7/, works reasonably well.
$(document).ready(function() {
$("#autocomplete").autocomplete({
"open": function(e, ui) {
//using the 'open' event to capture the originally typed text
var self = $(this),
val = self.val();
//saving original search term in 'data'.
self.data('searchTerm', val);
},
"select": function(e, ui) {
var self = $(this),
keyPressed = e.keyCode,
keyWasEnter = e.keyCode === 13,
useSelection = true,
val = self.data('searchTerm');
if (keyPressed) {
if (keyWasEnter) {
useSelection = false;
e.preventDefault();
window.setTimeout(function() {
//since there is apparently no way to prevent this
//contemptible menu from closing, re-open the menu
//using the original search term after this handler
//finishes executing (using 'setTimeout' with a delay
//of 0 milliseconds).
self.val(val);
self.autocomplete('search', val);
}, 0);
}
}
return useSelection;
},
"source": ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
});
原始:
这可以在小提琴中使用:
$(document).ready(function() {
$("#autocomplete").autocomplete({
"select": function(e, ui) {
var keyPressed = e.keyCode,
keyWasEnter = e.keyCode === 13,
useSelection = true;
console.log(e);
if (keyPressed) {
if (keyWasEnter) {
useSelection = false;
$(this).open(e, ui);
}
}
return useSelection;
},
"source": ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
});
这篇关于如何防止jQueryUI Autocomplete的默认键盘交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!