我一直在学习JS和JQuery,最近我一直在玩过滤内容,现在我尝试使用几个过滤器来显示和隐藏表格中的行,检查JSFiddle。我的问题是,我使用<select>
元素按名称Search FN
进行过滤,其中一个选项为Mark
,但如果我选择Mark
,则会向我显示名称Marky
所以如何只显示Mark
行?完全匹配。
HTML
<div class="row">
<span class="col-xs-3">
<span class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</span>
<input class="form-control" id="searchbox" type="text" name="searchbox" placeholder="Search" />
</span>
</span>
<span class="col-xs-3">
<select class="form-control" id="listfirst" name="listfirst">
<option value="" selected="selected">Search FN</option>
<option value="1">Mark</option>
<option value="2">Jacob</option>
<option value="3">Larry</option>
</select>
</span>
<span class="col-xs-3">
<select class="form-control" id="listlast" name="listlast">
<option value="" selected="selected">Search LN</option>
<option value="1">Otto</option>
<option value="2">Thornton</option>
<option value="3">the Bird</option>
</select>
</span>
<span class="col-xs-3">
<select class="form-control" id="listuser" name="listuser">
<option value="" selected="selected">Search UN</option>
<option value="1">@mdo</option>
<option value="2">@fat</option>
<option value="3">@twitter</option>
</select>
</span>
</div>
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td class="fn">Mark</td>
<td class="ln">Otto</td>
<td class="un">@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td class="fn">Jacob</td>
<td class="ln">Thornton</td>
<td class="un">@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td class="fn">Larry</td>
<td class="ln">the Bird</td>
<td class="un">@twitter</td>
</tr>
<tr>
<th scope="row">4</th>
<td class="fn">Marky</td>
<td class="ln">Thornton</td>
<td class="un">@twitter</td>
</tr>
</tbody>
</table>
这是脚本
JS
function filter() {
var box = $('#searchbox').val().toLowerCase();
var listf = $('#listfirst :selected').text().toLowerCase();
var listl = $('#listlast :selected').text().toLowerCase();
var listu = $('#listuser :selected').text().toLowerCase();
if (listf == 'search fn') {
listf = '';
}
if (listl == 'search ln') {
listl = '';
}
if (listu == 'search un') {
listu = '';
}
if (box != '' || listf != '' || listl != '' || listu != '') {
$('table > tbody > tr').hide().filter(function () {
var show = true;
var texttr = $(this).text().toLowerCase();
var textfn = $(this).find('td.fn').text().toLowerCase();
var textln = $(this).find('td.ln').text().toLowerCase();
var textun = $(this).find('td.un').text().toLowerCase();
if (box != '' && texttr.indexOf(box) == -1) {
show = false;
} else if (listf != '' && textfn.indexOf(listf) == -1) {
show = false;
} else if (listl != '' && textln.indexOf(listl) == -1) {
show = false;
} else if (listu != '' && textun.indexOf(listu) == -1) {
show = false;
}
return show;
}).show();
} else {
if (box == '') {
$('table > tbody > tr').show();
} else if (listf == '') {
$('table > tbody > tr').show();
} else if (listl == '') {
$('table > tbody > tr').show();
} else if (listu == '') {
$('table > tbody > tr').show();
}
}
};
$('#searchbox').keyup(filter);
$('#listfirst').change(filter);
$('#listlast').change(filter);
$('#listuser').change(filter);
感谢任何帮助=)抱歉我的英语不好。
编辑:这就是查看结束代码的方式:solution
答案 0 :(得分:1)
在过滤器函数的主体内,您使用indexOf()来确定是否将删除结果。因为字符串“Mark”在“Marky”内,所以将返回非负索引,这意味着在这种情况下不会删除行。而是尝试比较文字字符串值,如下所示:
if (box != '' && texttr != box) {
show = false;
这意味着整体功能将如下所示:
function filter() {
var box = $('#searchbox').val().toLowerCase();
var listf = $('#listfirst :selected').text().toLowerCase();
var listl = $('#listlast :selected').text().toLowerCase();
var listu = $('#listuser :selected').text().toLowerCase();
if (listf == 'search fn') {
listf = '';
}
if (listl == 'search ln') {
listl = '';
}
if (listu == 'search un') {
listu = '';
}
if (box != '' || listf != '' || listl != '' || listu != '') {
$('table > tbody > tr').hide().filter(function () {
var show = true;
var texttr = $(this).text().toLowerCase();
var textfn = $(this).find('td.fn').text().toLowerCase();
var textln = $(this).find('td.ln').text().toLowerCase();
var textun = $(this).find('td.un').text().toLowerCase();
if (box != '' && texttr != box) {
show = false;
} else if (listf != '' && textfn != listf) {
show = false;
} else if (listl != '' && textln != listl) {
show = false;
} else if (listu != '' && textun != listu) {
show = false;
}
return show;
}).show();
} else {
if (box == '') {
$('table > tbody > tr').show();
} else if (listf == '') {
$('table > tbody > tr').show();
} else if (listl == '') {
$('table > tbody > tr').show();
} else if (listu == '') {
$('table > tbody > tr').show();
}
}
};
$('#searchbox').keyup(filter);
$('#listfirst').change(filter);
$('#listlast').change(filter);
$('#listuser').change(filter);