问题
I try to create an autocomplete field with jQuery autocomplete widget, but it seems that does not work for some reason.
The code I use is the following:
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
}
);
And when I enter in the text field the text Pain I am getting as a result the text No search results.
What can be wrong with this code ?
回答1:
As it is clearly stated in the docs, your fields must be labeled label and value.
Array: An array can be used for local data. There are two supported formats:
An array of strings: [ "Choice1", "Choice2" ] An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]
EDIT : And as it has been pointed out, the input's id is autocomplete, not specialties.
来源:https://stackoverflow.com/questions/24970352/jquery-autocomplete-doesnt-work-with-key-value-pair-array