简体   繁体   中英

JSFiddle - Why is this JQuery Autocomplete not working?

What is wrong with this piece of code? Why does this JSfiddle not work?

HTML

<input id="university" name="university" type="text" />

Javascript

 $(function () {

        var jsonsample = [{
            "name": "Stanford University"
        }, {
            "name": "Santa Clara University"
        }]

        $('#university').autocomplete({

            minLength: 2,

            source: 'jsonsample',

            focus: function (event, ui) {
                $('#university').val(ui.item.name);
                return false;
            },

            select: function (event, ui) {

                $('#university').val(ui.item.name);

                return false;
            }
        })


            .data("autocomplete")._renderItem = function (ul, item) {
            return $("<li></li>")
                .data("item.autocomplete", item)

                .append("<a>" + item.name + "</a>")
                .appendTo(ul);
        };

    });

jsfiddle.net/venomoustoad/9yBcZ

Why do i get 'please use POST request'?

I am ideally looking for autocomplete to populate with the names in the jsonsample object.

You have the source as a string, it should not have the quotes

source: 'jsonsample',

remove them

source: jsonsample,

You seemed to mistype the demo

.data( "autocomplete" )._renderItem = function( ul

It should be

.data( "ui-autocomplete" )._renderItem = function( ul

and you need to have a value in your array

    [{
        "value": "Stanford University"
        "name": "Stanford University"
    }, {

JSFiddle: http://jsfiddle.net/NsBGH/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM