本文介绍了jQuery自动完成功能不适用于键值对数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用jQuery自动完成小部件创建一个自动完成字段,但是由于某些原因,它似乎不起作用.
I try to create an autocomplete field with jQuery autocomplete widget, but it seems that does not work for some reason.
我使用的代码如下:
HTML
<input type="text" id="specialties" />
JavaScript
var $specialties = [
{
id : 107,
name : 'Painting'
},
{
id : 158,
name : 'Reading'
}
];
var $specialty_text_field = $('#specialties');
$specialty_text_field.autocomplete(
{
source : $specialties,
minLength : 3
}
);
当我在文本字段中输入文本Pain
时,结果是文本No search results.
And when I enter in the text field the text Pain
I am getting as a result the text No search results.
此代码有什么问题?
推荐答案
="noreferrer">文档,您的字段必须标记为label
和value
.
As it is clearly stated in the docs, your fields must be labeled label
and value
.
An array of strings: [ "Choice1", "Choice2" ]
An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]
EDIT :正如已经指出的那样,输入的ID是自动填充,而不是专业.
EDIT : And as it has been pointed out, the input's id is autocomplete, not specialties.
这篇关于jQuery自动完成功能不适用于键值对数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!