2010-12-13 43 views

回答

2

最簡單的方法是使用您自己的_renderMenu函數來擴展jQuery UI Autocomplete。

http://jqueryui.com/demos/autocomplete/#default

HTML:

<div class="ui-widget"> 
    <label for="tags">Tags: </label> 
    <input id="tags"> 
</div> 

的jQuery:

$(function() { //DOM Ready 
    var availableTags = [ 
     "ActionScript", 
     "AppleScript", 
     "Asp", 
     "BASIC", 
     "C", 
     "C++", 
     "Clojure", 
     "COBOL", 
     "ColdFusion", 
     "Erlang", 
     "Fortran", 
     "Groovy", 
     "Haskell", 
     "Java", 
     "JavaScript", 
     "Lisp", 
     "Perl", 
     "PHP", 
     "Python", 
     "Ruby", 
     "Scala", 
     "Scheme"]; 

    $.widget("custom.customcomplete", $.ui.autocomplete, { 
     // our fancy new _renderMenu function adds the header and footers! 
     _renderMenu: function(ul, items) { 
      var self = this; 
      $.each(items, function(index, item) { 
       if (index == 0) 
        ul.append('<li><input type="checkbox"> I\'m at the top! Choose me!</li>'); 
       self._renderItem(ul, item); 
       if(index == items.length - 1) 
        ul.append('<li><input type="checkbox"> I\'m at the bottom! Choose me!</li>'); 
      }); 
     } 
    }); 

    // note the new 'widget', extended from autocomplete above 
    $("#tags").customcomplete({ 
     source: availableTags 
    }); 

}); 

看到這裏工作的例子:http://jsfiddle.net/kJUdt/

+0

偉大的答案,真是幫了我很多很多的感謝 – LenPopLilly 2012-04-26 10:04:40

+0

我建議使用append/prepend運算符而不是「if」和「check length」: '_renderMenu:function(ul,items){ var that = this; ul.prepend('

  • 我在頂部!選擇我!
  • '); $ .each(items,function(index,item){ that._renderItemData(ul,item); }); ul.append('
  • 我在底部!選擇我!
  • '); },' – 2013-07-03 16:30:41