如何使用jQuery搜索字符串筛选结果
How to filter results using jQuery search string
我有一个网站,它有一个用户配置文件列表,每个用户配置文件都在一个单独的div
中,类为user-profile
,每个用户都有一个与其名称相同的唯一id。所有这些都在一个#reporting
容器中。例如
<div id="reporting">
<div class="user-profile" id="John Smith">...content...</div>
<div class="user-profile" id="Jane Smith">...content...</div>
<div class="user-profile" id="Tom Nolan">...content...</div>
</div>
然后我有一个输入,我正在尝试用它来过滤结果。我希望用户输入一个字符串,如果该字符串不包含在元素的ID中,则使user-profiles
淡出。
使用上面的示例,如果访问者输入搜索字符串"Smith",John和Jane Smith都将保持可见,但Tom Nolan div
将淡出。如果访客进入汤姆,简和约翰·史密斯都会消失,但汤姆·诺兰仍然可见。
我正在尝试使用jQuery来实现这一点。我找到了这个链接:http://dinowebs.net/index.php/highlighting-matching-text-in-search-results-with-jquery/,但这几乎是我试图达到的相反效果,我不知道如何根据我的要求进行修改。任何帮助都将不胜感激!
$(':input[name=filter]').on('input',function() {
//get value just typed into textbox -- see .toLowerCase()
var val = this.value.toLowerCase();
//find all .user-profile divs
$('#reporting').find('.user-profile')
//find those that should be visible
.filter(function() {
return $(this).data('id').toLowerCase().indexOf( val ) > -1;
})
//make them visible
.show()
//now go back and get only the visible ones
.end().filter(':visible')
//filter only those for which typed value 'val' does not match the `data-id` value
.filter(function() {
return $(this).data('id').toLowerCase().indexOf( val ) === -1;
})
//fade those out
.fadeOut();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="reporting">
<div class="user-profile" data-id="John Smith">...content...1</div>
<div class="user-profile" data-id="Jane Smith">...content...2</div>
<div class="user-profile" data-id="Tom Nolan">...content...3</div>
</div>
<input type="text" name="filter"/>
// build an array of values
var data = [];
jQuery('.user-profile').each(function () {
var upid = jQuery(this).attr('id'); // user profile id
data.push(upid);
});
// on change of text input get value of the text input then analyze the reporting div children ids (data array), fade out any who has an id that doesn't contain the letter(s)
jQuery('[name="filter"]').change(function (e) {
var sv = jQuery(this).val();
// check value against data array values
for (var i = 0; i < data.length; i++) {
var tupid = data[i]; // this user profile id
var re = new RegExp(sv, "gi"); // make a regex
var check = tupid.match(re); // see if there is a match
var theid = '#'+tupid; // build actual id
// if this user profile id doesn't match something in the input fade out, if it does fade in
if (Array.isArray(check) === false) {
jQuery(theid).fadeOut();
}else{
jQuery(theid).fadeIn();
}
}
});
这是html——注意,对于这个解决方案,您需要在名称之间加下划线,或者将它们放在一起。它有效。。它很快。我还没有使用数据id,因为我主要使用php。不管怎样,这是小提琴来展示它的作用--->http://jsfiddle.net/eot5tfaq/
<div id="reporting">
<div class="user-profile" id="JohnSmith">...content...</div>
<div class="user-profile" id="JaneSmith">...content...</div>
<div class="user-profile" id="TomNolan">...content...</div>
</div>
<input name="filter" type="text" placeholder="Enter Your Search" />
$(document).ready(function(){
$('#filter').keyup(function(){
var value = $(this).val().toLowerCase();
$('#reporting .user-profile').filter(function(){
$(this).toggle($(this).attr('data-id').toLowerCase().indexOf(value) > -1);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="reporting">
<div class="user-profile" data-id="John Smith">...content...1</div>
<div class="user-profile" data-id="Jane Smith">...content...2</div>
<div class="user-profile" data-id="Tom Nolan">...content...3</div>
</div>
<input type="text" name="filter" id="filter"/>
$(document).ready(function(){
$('#filter').keyup(function(){
var value = $(this).val().toLowerCase();
$('#reporting .user-profile').filter(function(){
$(this).toggle($(this).attr('data-id').toLowerCase().indexOf(value) > -1);
});
});
});
这是HTML
<div id="reporting">
<div class="user-profile" data-id="John Smith">...content...1</div>
<div class="user-profile" data-id="Jane Smith">...content...2</div>
<div class="user-profile" data-id="Tom Nolan">...content...3</div>
相关文章:
- 计数HTML表中筛选的结果
- 返回带有筛选结果的新JSON对象
- 通过XML结果进行筛选
- 使用复选框筛选结果
- 复选框筛选结果
- 使用Javascript从DB中筛选结果,这将使DIV重新加载它'基于新参数的内容
- 使用jquery或JS从现有值集中筛选结果
- 如果文本筛选器在ng重复中未返回结果,则显示消息
- 如何根据其他下拉列表中的选择筛选下拉结果
- Sequelize是在筛选属性时对结果进行分组
- j查询筛选结果
- 使用单个 ng 模型筛选结果并显示筛选方法
- 使用自定义按钮筛选结果
- 如何从视图中的控制器筛选结果
- 如何使用jQuery搜索字符串筛选结果
- 使用可选逻辑运算符筛选结果的即时搜索
- 根据RegEx模式筛选结果
- jQuery为具有相同名称的类筛选结果
- 甚至在选择下拉列表中使用.change来筛选结果
- 根据复选框筛选结果