回答

7

有一個內置的方式做到這一點:只要提供的功能在自動完成插件的source選項:

var items = ['Foo', 'Bar', 'Hello', 'Goodbye', '1234']; 


$("#autocomplete").autocomplete({ 
    source: function(request, response) { 
     // The term the user searched for; 
     var term = request.term; 

     // Extract matching items: 
     var matches = $.grep(items, function(item, index) { 
      // Build your regex here: 
      return /\d+/.test(item); 
     }); 

     // let autocomplete know the results: 
     response(matches); 
    } 
}); 

http://jsfiddle.net/RSyrX/

注意,這由於我使用了簡單的正則表達式,因此示例將始終返回「1234」。更有用的東西可能是基於術語構建正則表達式(也是可能的)。

這實際上與小部件本身過濾結果的方式非常相似。請查看this line瞭解過濾器功能和this line以查看如果您提供數組作爲source選項,它是如何調用的。

+0

+1'AwesomeSolution(true);' – pixelbobby 2011-05-18 17:11:21

+0

@pixelbobby:Thanks :) – 2011-05-18 18:57:08

+0

嗨,再次感謝您的幫助! – user474632 2011-05-18 19:56:37

1

我創建了一個只包含標籤字符串「jQuery的」結果呈現的例子:

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"} 
]; 

$("input").autocomplete({ 
    source: projects 
}).data("autocomplete")._renderItem = function(ul, item) { 

    // this is where you can implement your regex 
    if (item.label.indexOf("jQuery") !== -1) { 
     return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "<br>" + item.desc + "</a>").appendTo(ul); 
    } 
}; 

Demo here.

1

雅您可以覆蓋jQueryUI的的自動完成的默認行爲。在你的控制器中,你應該編寫你的服務器端邏輯來生成結果集。 jQuery自動完成默認使用q作爲參數。使用它可以獲取值並生成將成爲列表的結果集。我認爲這會給你一個想法。自動完成只顯示每個鑰匙扣上的結果。你應該顯示邏輯

照顧