在Html中过滤表行 [英] Filter Table Row in Html
本文介绍了在Html中过滤表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个html页面,其中包含一个搜索文本和一个表格,表格中有一些数据。
I have created a html page which has a search text and a table below with some data on it in the table.
我使用了 JSFiddle 。
I used the code available on JSFiddle.
但它没有用。请提供类似示例的建议。
But it did not work. Please suggest something like the example shown.
我使用简单的html,CSS和Javascript编码。
I am using a simple html, CSS and Javascript coding.
在这个jQuery中使用:
In this jQuery is used:
var $rows = $('#table td');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
我可以更简单一些,因为这是一个混合应用程序吗?
Can I have something simpler since this is a hybrid application?
我使用以下代码。请检查
I am using the following code. Please check
<!DOCTYPE html>
<html>
<head>
<title>Search Box Example 2 - default placeholder text gets cleared on click</title>
<link rel="stylesheet" href="css/SalesTable.css">
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />
<!-- JAVASCRIPT to clear search text when the field is clicked -->
<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript">
var $rows = $('#table tr');
$('#tfq').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
</script>
<!-- CSS styles for standard search box with placeholder text-->
</head>
<body>
<!-- HTML for SEARCH BAR -->
<div id="tfheader">
<form id="tfnewsearch" method="get" action="http://www.google.com">
<input type="text" id="tfq" class="tftextinput2" name="q" size="21" maxlength="120" placeholder="Type to search"><input type="button" value="Search" class="tfbutton2">
</form>
<div class="tfclear"></div>
</div>
<br>
<br>
<div id="tableSalesOrder">
<table id="table">
<tr>
<th>
A
</th>
<th>
B
</th>
<th>
C
</th>
<th>
D
</th>
</tr>
<script language="javascript">
<!--
for(i =0 ;i<4 ; i++)
{
document.write('<tr>');
document.write('<td>12345</td>')
document.write('<td>34566</td>')
document.write('<td>345356</td>')
document.write('<td>Tyjhue</td>')
document.write('<td><form id="approveForm"><input type = "submit" value="ButtonA"></form></td>')
document.write('<td><form id="rejectForm"><input type = "submit" value="ButtonB"></form></td>')
document.write('<td><form id="detailForm"><input type = "submit" value="ButtonC"></form></td>')
document.write('</tr>')
}
//-->
</script>
</table>
</div>
</body>
</html>
推荐答案
包括Jquery
<head>
<style type="text/css">
body {padding: 20px;}
input {margin-bottom: 5px; padding: 2px 3px; width: 209px;}
td {padding: 4px; border: 1px #CCC solid; width: 100px;}
</style>
</head>
<body>
<input type="text" id="search" placeholder="Type to search">
<table id="table">
<tr> <td>Apple</td>
<td>Green</td>
</tr>
<tr> <td>Grapes</td>
<td>Green</td>
</tr>
<tr> <td>Orange</td>
<td>Orange</td>
</tr>
</table>
<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript">
var $rows = $('#table tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
</script>
</body>
运行代码,
这篇关于在Html中过滤表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文