简体   繁体   中英

jQuery Autocomplete Not Working With Array Datasource

I have created a jQuery autocomplete that works of an array generated by a PHP script:

var tags = [{"id":"77","label":"110826"},
            {"id":"76","label":"110667"},
            {"id":"74","label":"110808"}];

However the autocomplete box will not work - i am typing in values that are in the array but nothing is happening and I cannot figure out why.

Please see my fiddle here; http://jsfiddle.net/j4yB3/

Any help would be appreciated, thanks!

You hadn't closed your <label> tag (not that it really matters), but your Fiddle didn't have the jQueryUI and jQuery references.

Try THIS .

I have updated the fiddle code for the solution. Make sure you have added the jquery js and jquery ui js

<script>
  $(function() {
    var projects = [
      {
        value: "jquery",
        label: "jQuery",
        desc: "the write less, do more, JavaScript library",
        icon: "jquery_32x32.png"
      },
      {
        value: "jquery-ui",
        label: "jQuery UI",
        desc: "the official user interface library for jQuery",
        icon: "jqueryui_32x32.png"
      },
      {
        value: "sizzlejs",
        label: "Sizzle JS",
        desc: "a pure-JavaScript CSS selector engine",
        icon: "sizzlejs_32x32.png"
      }
    ];

    $( "#project" ).autocomplete({
      minLength: 0,
      source: projects,
      focus: function( event, ui ) {
        $( "#project" ).val( ui.item.label );
        return false;
      },
      select: function( event, ui ) {
        $( "#project" ).val( ui.item.label );
        $( "#project-id" ).val( ui.item.value );
        $( "#project-description" ).html( ui.item.desc );
        $( "#project-icon" ).attr( "src", "images/" + ui.item.icon );

        return false;
      }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
        .appendTo( ul );
    };
  });
  </script>

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