回答

21

您需要手動觸發search事件,並在插件的minLength選項爲零:

$("input").autocomplete({ 
    minLength: 0, 
    /* other options */ 
}).on("focus", function() { 
    $(this).autocomplete("search", ""); 
}); 

工作例如:http://jsfiddle.net/9JmVu/

+1

我不能得到這個在但我們正在使用的版本工作,以 「上的」 更換: .focus(函數(){$(本).autocomplete( 「搜索」, 「」 )}); 效果很好。 –

+0

您可能正在使用jQuery的早期版本。 –

+0

這可能沒有調用搜索事件? –

1

我想我明白了其實。如果將minLength設置爲0,然後觸發搜索「」,它將打開菜單。

 $(inputSelector).autocomplete(
       { 
        source: this.validConstructCodes, 
        minLength: 0, 
        autoFocus: true, 
        autoSelect: true 
       }); 
     $(inputSelector).focus(function(event) { 
     $(this).autocomplete("search" , ""); 
     }); 
1

從安德魯解釋你需要觸發事件。

但是,一旦您從ajax請求獲得結果,最好再次顯示結果而不是再次詢問服務器。 minLength值是獨立的,可以是服務器請求上推薦的2。

$("input").autocomplete({ 
    minLength: 2, 
    /* your options */ 
}).on("focus", function() { 
    /* the element with the search results */ 
    var uid = $("#ui-id-"+$(this).autocomplete("instance").uuid); 

    if(uid.html().length == 0) { 
     /* same as $(this).autocomplete("search", this.value); */ 
     $(this).keydown(); 
    } 
    else { 
     uid.show(); 
    } 
}); 
相關問題