简体   繁体   English

JSFiddle-为什么此JQuery自动完成功能不起作用?

[英]JSFiddle - Why is this JQuery Autocomplete not working?

What is wrong with this piece of code? 这段代码有什么问题? Why does this JSfiddle not work? 为什么这个JSfiddle不起作用?

HTML HTML

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

Javascript Java脚本

 $(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 jsfiddle.net/venomoustoad/9yBcZ

Why do i get 'please use POST request'? 为什么我会收到“请使用POST请求”?

I am ideally looking for autocomplete to populate with the names in the jsonsample object. 理想情况下,我正在寻找要使用jsonsample对象中的名称填充的自动填充功能。

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/ JSFiddle: http : //jsfiddle.net/NsBGH/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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