使用 jquery 自动完成功能匹配用户键入的输入
我正在使用 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 的源来做到这一点?换句话说,是否有一个属性允许我操作从 url 获取的 JSON,而无需为源代码编写回调函数?
提前致谢 !
I'm using jquery-ui autocomplete to validate some form inputs.
I've got this code :
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);
}
}
});
});
}
The goal is simple : associate the "id" value of the datas with an hidden field. When the user's input is not in the source, we put a "0". My problem is that when the user types everything and do not click or make use of the autocomplete, he could write a proper value but it would not be acknowleged as such.
I've seen how to avoid this problem if you store the source in a local array, for instance, but is there a way to do this with the source coming from the 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 ?
Thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一个解决方案:使用 Scott González 很棒 自动选择选项。工作完美,真的很棒。
Found a solution : using Scott González wonderful autoSelect option. Works perfectly, really great.