2011-11-07 39 views
5

我使用的,我從Rails應用程序的紅寶石填充jQuery的自動完成和我創建一個自定義的autcomplete像這樣:JQuery的自動完成 - 選擇第一項

$.widget("custom.code_complete", $.ui.autocomplete, { 
    _renderMenu: function(ul, items) { 
      var self = this, 
       currentCategory = ""; 
     $ul = ul; 
      $.each(items, function(index, item) { 
       if (item.category != currentCategory) { 
        ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>"); 
        currentCategory = item.category; 
       } 
       self._renderItem(ul, item); 
      }); 
     } 
    }); 

    $("#r-code").code_complete({ 
    source: "URL", 
    minLength: 2, 
    select: function(event, ui) { 
     $(".button-row").fadeIn(); 
     get_details(ui.item.url); 
    } 
    }); 

我重定向用戶從另一頁這是用於搜索代碼與在URL參數自動完成表單的頁面,這裏是JS做搜索:

function ac_search(code) { 
    $("#r-code").val(code); 
    $("#r-code").code_complete('search', code); 
} 

此執行搜索完美,顯示下拉結果列表。我正在嘗試讓腳本選擇列表中的第一個結果。

$(".ui-menu-item:first a").click(); 

此找到正確的元素在自動完成列表中,但是當我嘗試和模擬單擊它給出了這樣的錯誤:

TypeError: ui.item is null 

是否有可能以編程方式我已經嘗試通過選擇這樣做點擊自動填充結果列表中的第一項?

乾杯

EEF

回答

15

使用自動對焦:真

$(el).autocomplete({ 
     autoFocus: true 
     ... 
}); 
+0

你並不真正需要時,你可以設置自動對焦選項,因爲真正的這個擴展。 – Ziad

19
$(el).autocomplete({ 
    autoFocus: true 
}); 
相關問題