JQuery AutoComplete,手动选择第一项

时间:2017-09-18 16:47:44

标签: javascript jquery jquery-ui-autocomplete

我正在使用jQueryUI自动完成功能,我希望当我将焦点放在输入字段上时,会出现一个列表,并且列表的第一个元素被选中与文本一致。希望有人能提供帮助。提前感谢您的回复。

我的HTML:

<div class="ui-widget">
    <label for="tags">Tags:</label>
    <input id="tags"/>
</div>

我的javascript:

var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
    "Clojure",
    "COBOL",
    "ColdFusion",
    "Erlang",
    "Fortran",
    "Groovy",
    "Haskell",
    "Java",
    "JavaScript",
    "Lisp",
    "Perl",
    "PHP",
    "Python",
    "Ruby",
    "Scala",
    "Scheme"];

$("#tags").val("ja");
$("#tags").autocomplete({
    source: availableTags,
    autoFocus: true,
}).on( "focusin", function( event, ui ) {
    $(this).autocomplete("search", $(this).val());
});

https://jsfiddle.net/wx5a1q5k/3/

感谢

请有人帮忙

1 个答案:

答案 0 :(得分:0)

这是因为您尚未设置minLength属性,请在自动完成插件中设置minLength: 0

$("#tags").autocomplete({
     source: availableTags,
     autoFocus: true,
     minLength: 0
})

这是工作fiddle