[英]Match user's typed input with jquery autocomplete
我正在使用 jquery-ui 自动完成来验证某些表单输入。 我有这个代码:
function addAutoComplete(beacon, url) {
$(document).ready(function(){
$('input' + beacon).autocomplete({
source: url,
minLength:3,
select: function(event, ui) {
var selectedObj = ui.item;
$(beacon+'_autocomplete_id').val(selectedObj.id);
return false;
},
change: function(event, ui){
if (ui.item == null){
$(beacon+'_autocomplete_id').val(0);
} else {
$(beacon+'_autocomplete_id').val(ui.item.id);
}
}
});
});
}
目标很简单:将数据的“id”值与隐藏字段相关联。 当用户的输入不在源中时,我们放一个“0”。 我的问题是,当用户键入所有内容并且不单击或使用自动完成时,他可以写一个正确的值,但不会被这样确认。
例如,如果将源存储在本地数组中,我已经看到如何避免这个问题,但是有没有办法使用来自 url 的源来做到这一点? In other terms, is there a property that allow me to manipulate the JSON I go from the url without having to code my callback function for the source?
提前致谢 !
找到了解决方案:使用 Scott González 精彩的autoSelect option 。 完美运行,真的很棒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.