2015-04-07 50 views
-2

我有一個文本框,其ID爲txtAutocompletePortal文本框焦點上的自動完成菜單

如何打開文本框焦點上的自動完成菜單?

這裏是我的代碼:

Mod.SmartSearch = function (smartSearchOptions) { 

    if (typeof (smartSearchOptions) == "undefined" || smartSearchOptions == null) { smartSearchOptions = {}; }; 
    var sS = { 
     smartSearchOptions: $.extend({ 
      'autoFocus': false, 
      'source': function (reuqest, resonse) { 

      }, 
      'minLength': 0, 
      'delay': 100, 

      'select': function (event, ui) { 

      }, 
      'messages': { 
       noResults: '', 
       results: function() { } 
      }, 

     }, smartSearchOptions), 
     _form: '', 
     _searchFieldId: '', 
     _context: '', 
     _reset: {}, 
     _validate: {}, 
     _init: function() { 

      $(this.searchFieldId, this.context).autocomplete(this.smartSearchOptions); 

      $(this.searchFieldId, this.context).autocomplete("option", "appendTo", this._searchFieldId) 

     } 

    }; 
    return { 
     form: sS._form, 
     searchFieldId: sS._searchFieldId, 
     context: sS._context, 
     init: sS._init, 
     reset: sS._reset, 
     validator: sS._validate, 
     smartSearchOptions: sS.smartSearchOptions 
    }; 
}; 





var source = function (request, response) { 
        if (request.term.search(/[a-z A-Z 0-9]\s/) > 0) { 
         return; 
        } 
        else { 
         $.ajax({ 
          url: url + "/" + agentId + "/" +     
          Mod.ExtractLast(request.term), 
          dataType: "json", 
          success: function (data) { 
           response($.map(data, function (item) { 
            return { 
             label: item.label, 
             value: item.value 
            } 
           })); 
          } 
         }); 
        } 
       }; 
       var select = function (event, ui) { 
        event.preventDefault(); 
        if (ui.item) { 
         App.ContactInfo.portalId = ui.item.value; 
         App.ContactInfo.portalName = ui.item.label; 

         selctedPortalId = ui.item.value; 
         selctedPortalName = ui.item.label; 

         this.value = ui.item.label; 
        } 
       }; 

       var ss = Mod.SmartSearch({ source: source, select: select }); 
       ss.searchFieldId = '#txtAutocompletePortal'; 
       ss.context = '#PortalBodySrch'; 
       ss.init(); 
+0

這真的不清楚你指的是用「文本框」是什麼 – itdoesntwork

+0

https://github.com/FREE-FROM-CMS/AutoComplete是否解決了這個問題? –

回答

0

爲了jQuery Autocomplete彈出當用戶點擊文本框,使用此:

var field = $(/*whatever selector */); 

field.autocomplete({ 
    minLength: 0, 
    /* extra settings, as needed */ 
}).focus(function(){ 
    $(this).data("uiAutocomplete").search($(this).val()); 
});