简体   繁体   English

如果找不到匹配的值,jQuery-ui自动完成功能会显示所有值

[英]jQuery-ui Autocomplete show all values if no matching value found

Is it possible with jQuery Auto-complete, to make it so that if there are 'source:' values available, but they don't match what you are typing, then just show all the sources at once? 是否有可能使用jQuery自动完成,以便如果有'source:'值可用,但它们与您输入的内容不匹配,那么只需一次显示所有源?

IE, given the following code, if I type in "pineapple", how do you show all the programming languages instead of none of them? IE,给定以下代码,如果我输入“菠萝”,你如何显示所有编程语言而不是它们?

<script>
$(function() {
    var availableTags = [
        "JavaScript",
        "Perl",
        "PHP",
        "Python",
        "Ruby"
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});
</script>
<input type="text" id="tags" />

Use the source property with a custom function. source属性与自定义函数一起使用。 The custom function shown below mimics autocomplete original behavior, searching for the typed text as substring inside available tags. 下面显示的自定义函数模仿自动完成原始行为,在可用标记内搜索键入的文本作为子字符串。 If no match is found, all available tags are returned. 如果未找到匹配项,则返回所有可用标记。

$(function() {
    var availableTags = [
        "JavaScript",
        "Perl",
        "PHP",
        "Python",
        "Ruby"
    ];
    $("#tags").autocomplete({
        source: function(request, response) {
            var term = request.term.toLowerCase();
            var matchingTags = $.grep(availableTags, function(tag) {
                return tag.toLowerCase().indexOf(term) >= 0;
            });
            response(matchingTags.length ? matchingTags : availableTags);
        }
    });
});​

demo here 在这里演示

Just write a custom source callback. 只需编写自定义source回调。

For example: 例如:

source: function(req, res){
    res(['w00t', 'yay']);
}

See DOCs 见DOC

In your case (pseudocode): 在你的情况下(伪代码):

source: function(req, res){
     //find `req` in array
     if not found:
     res(availableTags);
     else:
     res({subset of availableTags});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM