jQuery autocomplete doesn't work with key value pair array

陌路散爱 提交于 2020-01-25 03:51:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!