jQuery表格筛选是指使用jQuery库来实现对HTML表格中的数据进行筛选的功能。通过用户输入的关键词,动态地显示或隐藏表格中的行,从而帮助用户快速找到所需的信息。
以下是一个简单的jQuery表格筛选示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Table Filter</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<input type="text" id="searchInput" placeholder="Search...">
<table id="dataTable">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>25</td>
<td>New York</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>30</td>
<td>Los Angeles</td>
</tr>
<tr>
<td>Alice Johnson</td>
<td>28</td>
<td>Chicago</td>
</tr>
</tbody>
</table>
<script>
$(document).ready(function() {
$('#searchInput').on('keyup', function() {
var value = $(this).val().toLowerCase();
$('#dataTable tbody tr').filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
</body>
</html>
$(document).ready()
中编写筛选逻辑。通过以上方法,可以有效解决jQuery表格筛选过程中遇到的常见问题。
领取专属 10元无门槛券
手把手带您无忧上云