jQuery 过滤器不适用于 Bootstrap 手风琴
jQuery filter not working with Bootstrap accordion
我正在使用 Bootstrap 3 的手风琴来显示一些信息。它很好用。但是,我的列表可能很长,所以我使用 jQuery 过滤器来尝试过滤结果。但是,当我执行过滤器搜索时,它会扭曲结果。例如这里是 returning un-collapsed 项。
我希望它只是 return 处于折叠状态的手风琴元素,并且只使用项目的标题。
这里是 HTML:
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#BRSEASMB">
<span class="glyphicon glyphicon-bed"> Test 1 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="BRSEASMB" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo lorem<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEA100">
<span class="glyphicon glyphicon-bed"> Test 2 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="KCSEA100" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
2 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEAHMS">
<span class="glyphicon glyphicon-bed"> Test 3 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="KCSEAHMS" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
</div>
这是我的 JS:
$(document).ready(function(){
$("#search-filter").on("keyup", function() {
var value = $(this).val().toLowerCase();
$(".panel-group *").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
在此先感谢您的帮助。
试试下面的代码片段
$(document).ready(function(){
$("#search-filter").on("keyup", function() {
var unicode = event.charCode ? event.charCode : event.keyCode;
if (unicode == 27) { $(this).val(""); }
var searchKey = $(this).val().toLowerCase();
$('.accordion-toggle').each(function() {
var cellText = $(this).text().toLowerCase();
var accordion = $('#accordion panel');
if (cellText.indexOf(searchKey) >= 0) {
$(this).parent().parent().show();
} else {
$(this).parent().parent().hide();
$('.panel-collapse.in').collapse('hide');
}
});
});
});
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.0/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<br/>
<div class="row-fluid text-left">
<span class="clearable">
<input type="text" id="search-filter" placeholder="Type to Search" />
<span class="icon_clear">x</span>
</span>
</div>
<br/>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#BRSEASMB">
<span class="glyphicon glyphicon-bed"> Test 1 <span class="badge">1</span></span>
</a>
</h4>
</div>
<div id="BRSEASMB" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo lorem<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEA100">
<span class="glyphicon glyphicon-bed"> Test 2 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="KCSEA100" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
2 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEAHMS">
<span class="glyphicon glyphicon-bed"> Test 3 <span class="badge">1</span></span>
</a>
</h4>
</div>
<div id="KCSEAHMS" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我正在使用 Bootstrap 3 的手风琴来显示一些信息。它很好用。但是,我的列表可能很长,所以我使用 jQuery 过滤器来尝试过滤结果。但是,当我执行过滤器搜索时,它会扭曲结果。例如这里是 returning un-collapsed 项。
我希望它只是 return 处于折叠状态的手风琴元素,并且只使用项目的标题。
这里是 HTML:
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#BRSEASMB">
<span class="glyphicon glyphicon-bed"> Test 1 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="BRSEASMB" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo lorem<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEA100">
<span class="glyphicon glyphicon-bed"> Test 2 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="KCSEA100" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
2 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEAHMS">
<span class="glyphicon glyphicon-bed"> Test 3 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="KCSEAHMS" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
</div>
这是我的 JS:
$(document).ready(function(){
$("#search-filter").on("keyup", function() {
var value = $(this).val().toLowerCase();
$(".panel-group *").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
在此先感谢您的帮助。
试试下面的代码片段
$(document).ready(function(){
$("#search-filter").on("keyup", function() {
var unicode = event.charCode ? event.charCode : event.keyCode;
if (unicode == 27) { $(this).val(""); }
var searchKey = $(this).val().toLowerCase();
$('.accordion-toggle').each(function() {
var cellText = $(this).text().toLowerCase();
var accordion = $('#accordion panel');
if (cellText.indexOf(searchKey) >= 0) {
$(this).parent().parent().show();
} else {
$(this).parent().parent().hide();
$('.panel-collapse.in').collapse('hide');
}
});
});
});
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.0/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<br/>
<div class="row-fluid text-left">
<span class="clearable">
<input type="text" id="search-filter" placeholder="Type to Search" />
<span class="icon_clear">x</span>
</span>
</div>
<br/>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#BRSEASMB">
<span class="glyphicon glyphicon-bed"> Test 1 <span class="badge">1</span></span>
</a>
</h4>
</div>
<div id="BRSEASMB" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo lorem<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEA100">
<span class="glyphicon glyphicon-bed"> Test 2 <span class="badge">1</span>
</a>
</h4>
</div>
<div id="KCSEA100" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
2 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#KCSEAHMS">
<span class="glyphicon glyphicon-bed"> Test 3 <span class="badge">1</span></span>
</a>
</h4>
</div>
<div id="KCSEAHMS" class="panel-collapse collapse">
<div class="panel-body">
<div class="list-group">
<div type="button" class="list-group-item">
1 foo<br>
foo<br>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>