最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery-ui Autocomplete show all values if no matching value found - Stack Overflow

programmeradmin4浏览0评论

Is it possible with jQuery Auto-plete, 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?

IE, given the following code, if I type in "pineapple", how do you show all the programming languages instead of none of them?

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

Is it possible with jQuery Auto-plete, 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?

IE, given the following code, if I type in "pineapple", how do you show all the programming languages instead of none of them?

<script>
$(function() {
    var availableTags = [
        "JavaScript",
        "Perl",
        "PHP",
        "Python",
        "Ruby"
    ];
    $( "#tags" ).autoplete({
        source: availableTags
    });
});
</script>
<input type="text" id="tags" />
Share Improve this question edited Dec 4, 2012 at 20:11 Salman Arshad 273k84 gold badges444 silver badges534 bronze badges asked Dec 4, 2012 at 19:21 TheFrackTheFrack 2,8738 gold badges29 silver badges49 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Use the source property with a custom function. The custom function shown below mimics autoplete 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").autoplete({
        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.

For example:

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

See DOCs

In your case (pseudocode):

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

评论列表(0)

  1. 暂无评论