自定义 ui 自动完成编辑为 url

custom ui autocomplete edit to url

我想编辑此代码:

原代码:(取自http://designshack.net/?p=40354

$(function(){
var currencies = [
{ value: 'Afghan afghani', data: 'AFN' },
{ value: 'Albanian lek', data: 'ALL' },
{ value: 'Algerian dinar', data: 'DZD' },
];

$('#autocomplete').autocomplete({
lookup: currencies,
onSelect: function (suggestion) {
var thehtml = '<strong>Currency Name:</strong> ' + suggestion.value +'<br> <strong>Symbol:</strong> ' + suggestion.data;
$('#outputcontent').html(thehtml);
}
});
});

我编辑的代码如下所示:

$(function(){
var mydata = [
{ value: 'google exmple', url: 'http://google.com/' },
{ value: 'yahoo exmple', url: 'http://yahoo.com/' },
{ value: 'bing exmple', url: 'http://bing.com/' },
];

$('#autocomplete').autocomplete({

// edit here
// i want that when i click on suggestion = redirect me to url
// and the maximum number of suggestion that shown = 7

});
});

谢谢。

未测试

$('#autocomplete').autocomplete({

    lookup: mydata,
    onSelect: function (suggestion) {
      window.location.href = suggestion.url;
    }

});

按照您的示例,您可以简单地重定向到 select 上的站点。似乎没有任何方法可以设置结果限制。

var mydata = [
  { value: 'google exmple', url: 'http://google.com/' },
  { value: 'yahoo exmple', url: 'http://yahoo.com/' },
  { value: 'bing exmple', url: 'http://bing.com/' },
];

$('#autocomplete').autocomplete({
  lookup: mydata,
  onSelect: function(data) {
    window.location.href = data.url;
  }
});