简体   繁体   English

检测输入中键入的单词和字母,并对具有相同字母的其他元素(div中的文本)进行排序

[英]Detect words and letters typed in input and sort other elements (text in div) with same letters

I want to make an input, in which you type words or letters to sort some kind of elements. 我要输入一个内容,在其中输入单词或字母来对某种元素进行排序。 For example: there are divs with different words in them, and when you type something in the input immediately sort the divs which have the same letters as in the input... 例如:有些div中有不同的单词,当您在输入中键入内容时,请立即对与输入中具有相同字母的div进行排序...

<input type="text" placeholder="Sort by...">

<div class="sortable-items">
    <div>dogs</div>
    <div>cats</div>
    <div>dogs</div>
    <div>dogs</div>
    <div>cats</div>
    <div>cats</div>
    <div>dogs</div>
</div>

If you listen for the keyup event on the input, the function will call every time you type or remove a letter. 如果您侦听输入中的keyup事件,则每次您键入或删除字母时,该函数都会调用。 You can then check if the input matches any of the divs' value, and hide/show it based on that. 然后,您可以检查输入是否与div的任何值匹配,并根据该值隐藏/显示它。

$('#search').keyup(function(){
    var searchTerm = $(this).val();
    $(".sortable-items div").each(function(){
        if($(this).text().match(searchTerm)){
            $(this).show();
        }else{
            $(this).hide();
        }
    });
});

You can test it in this fiddle: http://jsfiddle.net/h9sck2nh/1/ 您可以在此小提琴中对其进行测试: http : //jsfiddle.net/h9sck2nh/1/

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

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