gpt4 book ai didi

jQuery 过滤器 + 具有多个关键字的 Bootstrap

转载 作者:行者123 更新时间:2023-12-01 05:14:04 26 4
gpt4 key购买 nike

我希望修改引导过滤器以搜索多个关键字。

在下面的示例中,您只能搜索一个连续的关键字,例如“john”或“doe”。而我想要输入“oh do”(或“do oh”),它应该返回第一个“john doe john@example.com”行。

我一直在寻找其他一些示例,例如:thisthis 。但我无法完全让它工作,并且与上面发布的示例相比,第一个示例相当慢......

我设法弄清楚我是否可以通过切换来做到这一点:

$(this).toggle($(this).text().toLowerCase().indexOf(value1) > -1 && $(this).text().toLowerCase().indexOf(value2) > -1)

然后它会返回我正在寻找的结果,如果它总是两个关键字,那就很容易了,那么这将完美地工作。但在现实世界的场景中,在一个更大的表中,我们永远无法预测用户将输入多少个关键字,它可能是 4 或 5,也可能是 1...并且是编码新手,并且只一直在工作python/django 无助于理解和修改此 jquery 代码以实现结果...

有人可以尝试一下吗?

$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>Filterable Table</h2>
<p>Type something in the input field to search the table for first names, last names or emails:</p>
<input class="form-control" id="myInput" type="text" placeholder="Search..">
<br>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary@mail.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july@greatstuff.com</td>
</tr>
<tr>
<td>Anja</td>
<td>Ravendale</td>
<td>a_r@test.com</td>
</tr>
</tbody>
</table>

<p>Note that we start the search in tbody, to prevent filtering the table headers.</p>
</div>
</body>
</html>

最佳答案

不是短代码,但你可以尝试这个

function checkInput(element, value) {
value = value.split(' ');
for (var i = 0; i < value.length; i++) {
if (value[i] && element.text().toLowerCase().indexOf(value[i]) == -1) return false;
}
return true;
}

$(document).ready(function() {
$("#myInput").on("input", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle(checkInput($(this), value))
});
});
});

使用 es6 every() 所有浏览器和 IE9+

$(this).toggle(
value.split(' ').every(val => $(this).text().toLowerCase().indexOf(val) > -1)
)

关于jQuery 过滤器 + 具有多个关键字的 Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53763657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com