回车键上的jQuery自动完成操作 [英] JQuery AutoComplete action on Enter key
问题描述
我希望自动完成"的行为像这样.
I want the AutoComplete to behave like this.
当用户在文本框中键入内容时,什么都不会发生. 自动完成建议列表应仅在用户完成书写后出现 在文本框中,然后按Enter键.
When the user is typing something in the textbox nothing should be happen.. Autocomplete suggestion list should be appear only when the user has finished writing in the textbox and press the enter key..
任何想法如何执行..或在哪里更改代码..
Any idea how to do this.. Or where to change the code..
推荐答案
STEP:1
STEP : 1
通过更改具有以下签名的方法,将 jquery.ui.autocomplete.js 文件更改为接受Enter键
Change the jquery.ui.autocomplete.js file to accept the enter key by changing the method having signature as following
.bind("keydown.autocomplete",函数(事件)
.bind("keydown.autocomplete", function (event)
并更改其以下代码
case keyCode.ENTER:
case keyCode.NUMPAD_ENTER:
// when menu is open and has focus
if (self.menu.active) {
// #6055 - Opera still allows the keypress to occur
// which causes forms to submit
suppressKeyPress = true;
event.preventDefault();
}
//passthrough - ENTER and TAB both select the current element
收件人
case keyCode.ENTER:
case keyCode.NUMPAD_ENTER:
// when menu is open and has focus
if (self.menu.active) {
// #6055 - Opera still allows the keypress to occur
// which causes forms to submit
suppressKeyPress = true;
event.preventDefault();
}
else {
clearTimeout(self.searching);
self.searching = setTimeout(function () {
// only search if the value has changed
self.selectedItem = null;
self.search(null, event);
}, self.options.delay);
}
//passthrough - ENTER and TAB both select the current element
步骤:2 将自动完成绑定更改为
STEP : 2 Changing autocomplete binding as
$('.SearchAddresses').autocomplete({
// Your bind code by setting required parameters
search: function (event, ui) {
var key = CheckBrowser(event);
if (key == 13)
return true;
else
return false;
}
});
function CheckBrowser(e) {
if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return key;
}
设置:3 如果您在asp.net表单控件中使用它
SETP : 3 If you are using it inside the asp.net form control
然后也将其包括在内.
$(document).ready(function () {
$("form").keypress(function (e) {
var key = CheckBrowser(e);
if (key == 13) {
e.preventDefault();
return false;
}
else {
return true;
}
});
});
这篇关于回车键上的jQuery自动完成操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!