JQuery UI Autocomplete - Have the menu open when user clicks in the text box

南笙酒味 提交于 2019-12-07 05:24:00

问题


I'm using this JQuery autocomplete widget.

How can I make it automatically open the menu when the user clicks in the text box? I want the user to see all the options.


回答1:


You need to trigger the search event manually and set the minLength option on the widget to zero:

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

Working example: http://jsfiddle.net/9JmVu/




回答2:


I think I got it actually. If you set minLength to 0, and then trigger a search for "", it opens the menu.

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



回答3:


As explained from Andrew you need to trigger the event.

But once you got the result from an ajax request, it's better to show the results again instead of asking the server again. The minLength value is independent, could be 2 as recommended on server requests.

$("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();
    }
});


来源:https://stackoverflow.com/questions/8401734/jquery-ui-autocomplete-have-the-menu-open-when-user-clicks-in-the-text-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!