作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种方法来实时搜索我的网格+要过滤的复选框。我有一个包含学生的网格(照片和姓名)。我想要的是有一个复选框,可以过滤学生所在的不同类(class)。还有一个搜索栏,我可以在其中输入学生姓名。
我的两个元素都在工作,一个搜索栏和一个要过滤的复选框。但他们不一起工作。
当我输入 peter 时,它会像我想要的那样显示 peter,但是当我检查 A 时,我会看到 A 类的所有学生。当然,我仍然应该只看到 peter。当我取消选中 A 时,什么也没有显示。我该怎么做才能让两者一起工作?看我的 fiddle 。
$(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
$("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(0);
// 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);
});
});
$(document).ready(function () {
$('.studentList > li.' + $(this).attr('rel')).show();
$('div.tags').find('input:checkbox').live('click', function () {
$('.studentList > li').hide();
$('div.tags').find('input:checked').each(function () {
$('.studentList > li.' + $(this).attr('rel')).show();
});
});
});
最佳答案
您可以为已隐藏的学生添加类(class)并从显示行为中排除:
$(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
$("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(0).addClass('hidden');
// Show the list item if the phrase matches and increase the count by 1
} else {
$(this).show().removeClass('hidden');
count++;
}
});
// Update the count
var numberItems = count;
$("#filter-count").text("Number of Comments = " + count);
});
});
$(document).ready(function () {
$('.studentList > li.' + $(this).attr('rel')).show();
$('div.tags').find('input:checkbox').live('click', function () {
$('.studentList > li').hide();
$('div.tags').find('input:checked').each(function () {
$('.studentList > li.' + $(this).attr('rel')).not('.hidden').show();
});
});
});
jsfiddle:http://jsfiddle.net/wpxajkcw/
要解决取消选中 cehckbox 的问题:
$(document).ready(function () {
$('.studentList > li.' + $(this).attr('rel')).show();
$('div.tags').find('input:checkbox').live('click', function () {
if($('div.tags').find('input:checkbox:checked').length > 0){
$('.studentList > li').hide();
$('div.tags').find('input:checked').each(function () {
$('.studentList > li.' + $(this).attr('rel')).not('.hidden').show();
});
} else {
$('.studentList > li').not('.hidden').show();
}
});
});
更新了 jsfiddle: http://jsfiddle.net/wpxajkcw/1/
关于Jquery 实时搜索和复选框过滤器、网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30197907/
我是一名优秀的程序员,十分优秀!