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

javascript - How do I create a search filter text box in Jquery? - Stack Overflow

programmeradmin3浏览0评论

Assuming I have a div structure with a textbox:

<input id="filter" type="text" name="fname" /><br />
<input type="text">
<div id="listofstuff">
    <div class="anitem">
        <span class="item name">Dog</span>
        <span class="itemdescruption">AboutItem1</span>
    </div>
    <div class="anitem">
        <span class="item name">Doodle Bird</span>
        <span class="itemdescruption">AboutItem2</span>
    </div>
    <div class="anitem">
       <span class="item name">Cat</span>
       <span class="itemdescruption">AboutItem3</span>
    </div>
</div>

I want to make it so that initially.. say it shows 3 anitems, I want to make it so that if the user has any character typed into the search box, it would only show the divs that match with the entry the user is typing.

Ex. So if the user types "D", it would hide all other divs without "D" in the "item name". If the user types another letter "og", then the only div that would be shown is the div with" item name" of "Dog" If the user hits the delete key to remove the "g", then the two divs "Dog" and "Doodle Bird" would be show. If the user deletes everything they typed in the text box, then all the anitems would show up.

How do I acplish this if possible? I think I may have to use .live() but I'm really not sure.

Assuming I have a div structure with a textbox:

<input id="filter" type="text" name="fname" /><br />
<input type="text">
<div id="listofstuff">
    <div class="anitem">
        <span class="item name">Dog</span>
        <span class="itemdescruption">AboutItem1</span>
    </div>
    <div class="anitem">
        <span class="item name">Doodle Bird</span>
        <span class="itemdescruption">AboutItem2</span>
    </div>
    <div class="anitem">
       <span class="item name">Cat</span>
       <span class="itemdescruption">AboutItem3</span>
    </div>
</div>

I want to make it so that initially.. say it shows 3 anitems, I want to make it so that if the user has any character typed into the search box, it would only show the divs that match with the entry the user is typing.

Ex. So if the user types "D", it would hide all other divs without "D" in the "item name". If the user types another letter "og", then the only div that would be shown is the div with" item name" of "Dog" If the user hits the delete key to remove the "g", then the two divs "Dog" and "Doodle Bird" would be show. If the user deletes everything they typed in the text box, then all the anitems would show up.

How do I acplish this if possible? I think I may have to use .live() but I'm really not sure.

Share Improve this question edited Jul 25, 2018 at 4:19 Pathik Vejani 4,50111 gold badges65 silver badges109 bronze badges asked Jan 8, 2012 at 13:19 RolandoRolando 62.7k103 gold badges278 silver badges422 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

You will have to handle the keyup event (which is clicked after key is released) then use .filter() to find matching items, showing their parents.

$("#filter").bind("keyup", function() {
    var text = $(this).val().toLowerCase();
    var items = $(".item_name");

    //first, hide all:
    items.parent().hide();

    //show only those matching user input:
    items.filter(function () {
        return $(this).text().toLowerCase().indexOf(text) == 0;
    }).parent().show();
});

Live test case.

If you want to filter as the user types, you should attach an event handler to the textbox's keypress[API Ref] event. Everytime the user presses a key, you can filter[API Ref] the contents of #listofstuff and either show[API Ref] or hide[API Ref] it.

You want to handle the keyup event and i think you can do this:

$('div').each(function() {
   $(this).toggle($(this).children('span.itemname').text() == yourVar);
});
发布评论

评论列表(0)

  1. 暂无评论