autocomplete show all options on focus

时间秒杀一切 提交于 2019-12-22 04:09:27

问题


Iam using this plugin.

How do I show all the options available in the dropdown for an input field when it receives focus? Right now , I have to type something for the plugin to filter the options.

What I have right now

var $sessionTimes = "00:00 00:15 00:30 00:45 1:00 1:15".split(" ");
$(".autocompleteTime").autocomplete($sessionTimes);

<input type="text" class="autocompleteTime" size="5" />

回答1:


You have to set minChars to be 0, like this:

$('.autocompleteTime').autocomplete($sessionTimes, {minChars: 0});

Also note that you don't have to start variable name with a $, you could just write sessionTimes everywhere you use it and it would be okay. Probably coming from a PHP background? :)




回答2:


This is the correct answer:

    $('.autocompleteTime').autocomplete($sessionTimes, {minChars: 0})
    .focus(function () {
        $(this).autocomplete('search', $(this).val())
    });



回答3:


Check out jQuery Ui's Autocomplete combobox example:

http://jqueryui.com/demos/autocomplete/#combobox




回答4:


The selected answer is a bit old and didn't really work for me, so what worked for me was this:

$('#selector')
    //use minLength when initializing so that empty searches work
    .autocomplete({..., minLength: 0})
    //trigger the search on focus
    .focus(function(){
        $(this).autocomplete('search', $(this).val());
    })

Credits to the comment by @notJim above and this question: Display jquery ui auto-complete list on focus event, and to me




回答5:


That module has now been incorporated into the jQuery UI. This post covers how to deal with this problem now:

Jquery UI autocomplete; minLength:0 issue



来源:https://stackoverflow.com/questions/1464796/autocomplete-show-all-options-on-focus

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