在输入字段中按任何关键字时开始自动完成

时间:2018-11-15 10:41:35

标签: javascript html

当输入字段中有一些文本时,自动完成功能不起作用,并且我希望在按任意关键字(例如'#')的任何时候它都应在该输入字段中起作用。 我能够执行它在日志中显示的搜索部分,但是在页面上不可见,只有在输入字段为空时才可见。

1 个答案:

答案 0 :(得分:0)

您可以看到此示例。

我认为它可以帮助您

<input type="text" name="autocomplete" id="globalBCCAutoComplete" value="john">


$(document).ready(function() {
    let countries = [
       { value: '{contract_name}', data: 'Contact Name'},
       { value: '{user_company}', data: 'User Company' }
    ];

    //get current value from the textbox
    let initialTextValue = $('#globalBCCAutoComplete').val().trim();

    //append the values to the json object.
    $(countries).each(function(key, country) {
        country.value = initialTextValue + ' ' + country.value
    });

    //the rest of your code:
    $('#globalBCCAutoComplete').autocomplete({
        lookup: countries,
        onSelect: function (suggestion) {
            console.log('You selected: ' + suggestion.value + ', ' + suggestion.data);
        }
    });
});