JQuery UI自动完成 – 当用户在文本框中单击时,打开菜单

我正在使用这个JQuery自动完成小部件 。

当用户在文本框中单击时,如何让它自动打开菜单? 我希望用户看到所有选项。

您需要手动触发search事件并将窗口小部件上的minLength选项设置为零:

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

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

我想我实际上得到了它。 如果将minLength设置为0,然后触发搜索“”,则会打开菜单。

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

正如安德鲁所解释的,你需要触发事件。

但是一旦你从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(); } });