简体   繁体   English

jQuery的:仅显示匹配在自动填充下拉字符

[英]jQuery: Display only matching characters in autocomplete dropdown

I'm making my own dropdown script in jQuery. 我在jQuery中制作自己的下拉脚本。 Things are going well, but I'm running into a problem. 事情进展顺利,但我遇到了问题。 Whenever I enter any characters into the text field, all elements in the array I am searching are displayed, instead of the elements that match what's in the textbox. 每当我输入任何字符的文本字段,显示阵列我正在搜索中的所有元素,而不是匹配什么是在文本框中的元素。

For example, if I regex search for "ra" against my array of: 例如,如果我正则表达式搜索“RA”对我的数组:

var planes = [
    'Alara',
    'Fiora',
    'Innistrad',
    'Kamigawa',
    'Lorwyn',
    'Mirrodin',
    'Ravnica',
    'Shandalar',
    'Zendikar'
];

I should only see Ala ra , Fio ra , Innist ra d and Ra vnica in the autocomplete. 我应该只在自动完成中看到Ala ra ,Fio ra ,Innist ra d和Ra vnica。

Here's a Plunker. 这里有一个Plunker。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You need to compare to -1, so it will only show if it is found. 您需要与-1比较,因此仅在找到时才会显示。 You are currently comparing to 1 instead, so it returns true for all items. 您目前比较1代替,所以它返回的所有项目如此。 Probably a typo. 可能是拼写错误。 Good luck! 祝好运!

        else{
            //Grep used to search array but not alter the original.
            var results = $.grep(planes, function(item){
                return item.search(new RegExp(query, "i")) != *add a - here* 1;
            });
            console.log("Added " + query + " to cache.");
            cache[query] = results;
        }

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

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