如何仅在选中时显示自动填充选择?

时间:2017-01-10 18:23:19

标签: javascript jquery html

我有一个JavaScript代码,可以自动完成预测。

即使我将鼠标悬停在该预测上,此代码也会显示该单词。

实际上我只有在从列表中选择一个预测时才需要显示它。

在此处查看演示:JSFIDDLE

$(function () {
    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").autocomplete({
        source: availableTags
    });
});
$(window).load(function(){
    $('#tags').each(function(i, el) {
        var that = $(el);
        that.autocomplete({
            select: function( event , ui ) {
                alert( "You searched for: " + ui.item.label );//alerting the selection to the user
            }
        });
    });
});

这些线的含义是什么?有人可以解释一下吗?

$(window).load(function(){
    $('#tags').each(function(i, el) {
        var that = $(el);
        that.autocomplete({  
            select: function( event , ui ) {
                alert( "You searched for: " + ui.item.label );//alerting the selection to the user
            }
        });
    });
});

1 个答案:

答案 0 :(得分:0)

我认为你可能会让它变得更难。

$("#tags").autocomplete({
    source: availableTags,
    focus : function(event) { event.preventDefault(); },
    select: function(event, ui) {
        alert( "You searched for: " + ui.item.label); //alerting the selection to the user
    }
});

应该足够了。防止在focus事件上触发默认事件将阻止鼠标悬停在其上的元素显示在#tags输入中。