简体   繁体   中英

jQuery UI Autocomplete Search

I want to use jQuery UI autocomplete so the user can search for items.

I have a list of products that I need to make into JSON (or just include them in JavaScript like jQuery UI's demo has) but is there a way when the user selects an item, it redirects to the URL?

In jQuery UI's demo, they have:

$(function() {
  var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
    "Clojure",
    "COBOL",
    "ColdFusion",
    "Erlang",
    "Fortran",
    "Groovy",
    "Haskell",
    "Java",
    "JavaScript",
    "Lisp",
    "Perl",
    "PHP",
    "Python",
    "Ruby",
    "Scala",
    "Scheme"
  ];
  $( "#tags" ).autocomplete({
    source: availableTags
  });
});

Where can I add a URL for each item so when the user selects it, it'll redirect the page?

If you pass an object with a property named value, then jquery will use that as the display value. You can then use the select method on the autocomplete to handle the event:

var availableTags = [
    {value: "ActionScript", url:"http://www.google.com?q=ActionScript"},
    {value: "jquery", url:"http://www.google.com?q=jquery"}
    ];
    $( "#tags" ).autocomplete({
      source: availableTags,
        select:function(event, ui) {
        window.open(ui.item.url);        
    }
    });

Example fiddle: http://jsfiddle.net/9qePM/

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