最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery autocomplete doesn't work with key value pair array - Stack Overflow

programmeradmin2浏览0评论

I try to create an autoplete field with jQuery autoplete 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.autoplete(
    {
        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 ?

I try to create an autoplete field with jQuery autoplete 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.autoplete(
    {
        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 ?

Share Improve this question edited Jul 26, 2014 at 11:18 KodeFor.Me asked Jul 26, 2014 at 11:06 KodeFor.MeKodeFor.Me 13.5k28 gold badges103 silver badges172 bronze badges 5
  • 1 any console errors??? – Kartikeya Khosla Commented Jul 26, 2014 at 11:07
  • @KartikeyaKhosla no at all. My console is totally clear. – KodeFor.Me Commented Jul 26, 2014 at 11:09
  • @NaveenThally This is what I try but I get a console error that I cannot figure out. Here is the fiddle URL (jsfiddle/ZQ9T3). If somebody can help to fix the issue please? – KodeFor.Me Commented Jul 26, 2014 at 11:11
  • You have both #autoplete and #specialties, which should be? – Sergio Commented Jul 26, 2014 at 11:12
  • your souce needs to ba an array cosisting of Strings. you are giving an array consisting of objects... – Pinoniq Commented Jul 26, 2014 at 11:12
Add a ment  | 

1 Answer 1

Reset to default 6

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 autoplete, not specialties.

发布评论

评论列表(0)

  1. 暂无评论