使用html select对筛选表进行ajax刷新
Using html select to make ajax refresh to filter table
如标题所述,我想知道如何制作一个HTML选择框来发出ajax请求并使用它来过滤表。我搜索了很多,但没有找到一个好的解决方案。我怎么能让它过滤?
我在jsfiddle.net上的源代码:
<select>
<option value="all">All</option>
<option value="tim">Tim</option>
<option value="cook">Cook</option>
</select>
<br>
<br>
<br>
<table border="1">
<tr>
<th>Name</th>
<th>Year</th>
</tr>
<tr>
<td>Tim</td>
<td>2015</td>
</tr>
<tr>
<td>Cook</td>
<td>2015</td>
</tr>
</table>
您的示例有点奇怪,因为您只过滤到一行,但以下是您如何使其工作的方法。
首先,给<select>
和<table>
各一个id。我选择了一些通用的:
<select id='column-select'>
<option value="all">All</option>
<option value="tim">Tim</option>
<option value="cook">Cook</option>
</select>
<br>
<br>
<br>
<table id='data' border="1">
<tr>
<th>Name</th>
<th>Year</th>
</tr>
<tr>
<td>Tim</td>
<td>2015</td>
</tr>
<tr>
<td>Cook</td>
<td>2015</td>
</tr>
</table>
现在,只要使用一些Jquery来过滤掉你不想要的行,当列改变
$(document).ready(function() { // Do nothing until document is ready
$("#column-select").change(function() { // On <select> change...
var selection = $("#column-select").val();
if (selection == "all")
{
$("#data tr").show(); // Show all rows
} else
{
$("#data tr").each(function() { // For each row
if ($(this).find("th").length == 0) // If this is not a heading row
{
if ($(this).find("td").eq(0).text().toLowerCase() == selection)
{
// Show rows where the first column matches
$(this).show();
} else
}
// Hide rows where the first column does not match
$(this).hide();
}
}
});
}
});
});
相关文章:
- 如何通过ajax刷新JSF填充的javascript变量
- 如何使用AJAX刷新表
- 使用Ajax刷新页面属性
- Rails Ajax 刷新部分,同时持久化参数
- 通过ajax刷新页面
- 如何在不使用AJAX刷新的情况下发送表单的数据
- jQuery AJAX刷新没有'不能在iPhone上工作
- ajax刷新后保留select下拉值
- 可以't使用ajax刷新php表
- 如何使用jQuery和Ajax刷新
- 如何使用 AJAX 刷新 PHP 文件
- 检票口 - 使用 AJAX 刷新组件 - 文档元素后的垃圾
- 使用 AJAX 刷新 HTML 表
- 如果 ajax 刷新了绑定对象,则 .submit 不起作用
- 提交表单数据,而不使用 $ajax刷新页面
- 如何使用 Ajax 刷新
- ajax 刷新后调整大小函数出现问题
- 在 jsp 中使用 ajax 刷新按钮时刷新行的列
- Internet Explorer 9 中的 Ajax 刷新后速度变慢
- 高图表在 AJAX 刷新时呈现黑色