I have lots(30+) of Bootstrap panels, which are as follows (each has a different title and content):
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title">Title</h3>
</div>
<div class="panel-body">
Content
</div>
<div class="panel-footer">
Footer
</div>
</div>
And I have a search input
<input type="text" id="filter" placeholder="Filter Algorithms">
What I want to do it when someone types into the filter box it searches the title of the panels and filters them down as required. I have seen this done before, but I'm not quite sure where to start. This is the code I have so far:
$('#filter').keyup(function(){
$('body').find('.panel-title').find($('#filter').val());
});
I have lots(30+) of Bootstrap panels, which are as follows (each has a different title and content):
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title">Title</h3>
</div>
<div class="panel-body">
Content
</div>
<div class="panel-footer">
Footer
</div>
</div>
And I have a search input
<input type="text" id="filter" placeholder="Filter Algorithms">
What I want to do it when someone types into the filter box it searches the title of the panels and filters them down as required. I have seen this done before, but I'm not quite sure where to start. This is the code I have so far:
$('#filter').keyup(function(){
$('body').find('.panel-title').find($('#filter').val());
});
Share
Improve this question
asked Oct 8, 2013 at 12:06
Benedict LewisBenedict Lewis
2,8237 gold badges41 silver badges81 bronze badges
3
- What do you mean when you say "filters them down as required"? What's the expected behaviour? – André Dion Commented Oct 8, 2013 at 12:09
-
Well, if a user enters
foo
in the input, it hides all panels that don't containfoo
in the title. If they change it tobar
, it shows all those withbar
in the title etc. – Benedict Lewis Commented Oct 8, 2013 at 12:10 - Ok, the hiding functionality is what I was after. – André Dion Commented Oct 8, 2013 at 12:10
2 Answers
Reset to default 5Try this:
var $panels = $('.panel');
$('#filter').on('keyup', function() {
var val = this.value.toLowerCase();
$panels.show().filter(function() {
var panelTitleText = $(this).find('.panel-title').text().toLowerCase();
return panelTitleText.indexOf(val) < 0;
}).hide();
});
References
jQuery.filter()
: http://api.jquery./filter
this jquery plugin could be useful: https://github./Mixtags/jquery-filterbox
$('#list').filterbox({
container: '.list-group',
child: '.list-group-item',
childKey: '.list-group-item > .title'
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744749232a4591491.html
评论列表(0条)