我有以下代码:
container.autocomplete({
minLength: 2,
source: function(request, response) {
$.ajax({
type : 'POST',
url: HOST + "/lib/gets/getjsonclients.php",
dataType: "json",
data: {
q: request.term
},
success: function(data) {
ajaxCache[cachedTerm] = data;
response($.map(data, function(item) {
return {
label: item.value,
value: item.value
}
}));
}
});
},
select: function(event, ui) {
alert(ui.item.value);
this.close
},
//change: function(event, ui) {
// alert($(this).val());
//},
open: function() {
},
close: function() {
}
});
我想在输入新值(选择列表中已有的值)时捕获回车键事件。
我已尝试使用 .result 事件,但出现错误:Uncaught TypeError: Object # has no method 'result' 使用代码:
container.result(function(event, data, formatted) {
alert($(this).val());
});
变化:事件工作正常,但只有在我失去对自动完成的关注后才会触发,我需要在按下回车键时完成
感谢您的回答。
最佳答案
您应该将容器绑定(bind)到按键并捕获回车键(未测试但应该可以工作):
$(container).keypress(function(event) {
if(event.which == 13) {
$(this).autocomplete('search');
}
});
关于javascript - 新值的 JQuery 自动完成事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4722823/