0

首先,我必须承认我对 JS 或 Jquery 真的很糟糕,这很重要。我搜索了可以帮助我使用 jquery 过滤结果的代码。这是代码:

$(document).ready(function(){
    $("#filter").keyup(function(){  
        // Retrieve the input field text and reset the count to zero        
        var filter = $(this).val(), count = 0;         
        // Loop through the comment list      
        $(".commentlist li").each(function(){           
            // If the list item does not contain the text phrase fade it out    
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {     
                $(this).fadeOut();  
                // Show the list item if the phrase matches and increase the count by 1
            } else {     
                $(this).show();     
                count++;         
            }        
        });        
        // Update the count     
        var numberItems = count;
        $("#filter-count").text("Number of Comments = " + count);
    });
});

此代码适用于下面列出的无序列表:

  <ol class="commentlist">  <li>Comment #1</li>  <li>Comment #2</li></ol> 

现在这段代码在列表中工作得很好。不过,我的站点中有一个表,每行有 3 条记录。每个记录(单元格)代表用户配置文件。我希望这个精确的搜索功能可以在表格上使用,以便用户可以输入用户名并显示它。那可能吗?

谢谢大家,非常感谢支持,

4

1 回答 1

0
<script>
    $(document).ready(function(){
        $("#filter").keyup(function(){  
            // Retrieve the input field text and reset the count to zero        
            var filter = $(this).val(), count = 0;         
            // Loop through the comment list      
            $(".users td").each(function(){           
                // If the list item does not contain the text phrase fade it out    
                if ($(this).text().search(new RegExp(filter, "i")) < 0) {     
                    $(this).fadeOut();  
                    // Show the list item if the phrase matches and increase the count by 1
                } else {     
                    $(this).show();     
                    count++;         
                }        
            });        
            // Update the count     
            var numberItems = count;
            $("#filter-count").text("msg" + count);
        });
    });
</script>
于 2013-05-13T10:31:05.127 回答