假设我有..。
<form action="#">
<fieldset>
to:
<input type="text" name="search" value="" id="to" />
from:
<input type="text" name="search" value="" id="from" />
</fieldset>
</form>
<table border="1">
<tr class="headers">
<th class="bluedata"height="20px" valign="top">63rd St. & Malvern Av. Loop<BR/></th>
<th class="yellowdata"height="20px" valign="top">52nd St. & Lansdowne Av.<BR/></th>
<th class="bluedata"height="20px" valign="top">Lancaster & Girard Avs<BR/></th>
<th class="yellowdata"height="20px" valign="top">40th St. & Lancaster Av.<BR/></th>
<th class="bluedata"height="20px" valign="top">36th & Market Sts<BR/></th>
<th class="yellowdata"height="20px" valign="top">Juniper Station<BR/></th>
</tr>
<tr>
<td class="bluedata"height="20px" title="63rd St. & Malvern Av. Loop">
<table width="100%">
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:47am</td>
</tr>
</table>
</td>
<td class="yellowdata"height="20px" title="52nd St. & Lansdowne Av.">
<table width="100%">
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:47am</td>
</tr>
</table>
</td>
<td class="bluedata"height="20px" title="Lancaster & Girard Avs">
<table width="100%">
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:47am</td>
</tr>
</table>
</td>
<td class="yellowdata"height="20px" title="40th St. & Lancaster Av.">
<table width="100%">
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:47am</td>
</tr>
</table>
</td>
<td class="bluedata"height="20px" title="36th & Market Sts">
<table width="100%">
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:47am</td>
</tr>
</table>
</td>
<td class="bluedata"height="20px" title="Juniper Station">
<table width="100%">
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:17am</td>
</tr>
<tr>
<td>12:47am</td>
</tr>
</table>
</td>
</tr>
</table>现在,根据在文本框中键入的数据,我需要显示或隐藏表trs/tds。
因此,如果我在"to“框中输入63,在"from”框中输入juniper,我只需要按该顺序显示这两个trs/tds,其他的都不需要。
发布于 2010-04-29 02:12:16
我把这个代码块的一个小demo放在一起,但它适用于这个特定的情况:
$(function() {
$('#to,#from').bind('keyup change', function() {
var to = $('#to').val().toLowerCase();
var from = $('#from').val().toLowerCase();
var $th = $('#theTable').find('th');
// had to add the classes here to not grab the "td" inside those tables
var $td = $('#theTable').find('td.bluedata,td.yellowdata');
if (to.length == 0 || from.length == 0) {
// shortcut - nothing set, show everything
$th.add($td).show();
return;
}
$th.add($td).hide();
$th.each(function(idx) {
var txt = $(this).text().toLowerCase();
if ((txt.indexOf(to) != -1) || (txt.indexOf(from) != -1)) {
// the text had the to or the from in it - so show this tr
$(this).show();
// and show its corresponding td
$td.eq(idx).show();
}
});
});
});发布于 2010-04-29 04:04:11
在不更改代码的情况下,您可以尝试这样做。它将隐藏没有匹配项的列,但不会更改它们的顺序。它也只在找到两个或更多列匹配时才进行隐藏。事实上,你应该只在你需要帮助解决你已经尝试过的问题的地方发布东西,而不是让其他人来为你做这项工作。
<script type="text/javascript">/* <![CDATA[ */
function tableFilter()
{ // show / hide table data based in search filters
var loop=0, cnt=0, idx=[], obj, txt1=$("#to").val().toLowerCase(), txt2=$("#from").val().toLowerCase();
$("table:eq(0) tr.headers th").each(function(){ // for each header description
if ($(this).parents("table").length < 2) {
if (txt1 != "" && $(this).html().toLowerCase().indexOf(txt1) != -1) { cnt++; idx[loop] = true; }
if (txt2 != "" && $(this).html().toLowerCase().indexOf(txt2) != -1) { cnt++; idx[loop] = true; }
loop++;
}
});
if (txt1 != "" && txt2 != "" && cnt > 1) { // filter display of cells if 2 or more matches found
loop = 0;
$("table:eq(0) tr.headers th").each(function(){
if ($(this).parents("table").length < 2) {
$(this).css("display", (idx[loop]==true)? "" : "none"); // hide/show cell
loop++;
}
});
loop = 0;
$("table:eq(0) td").each(function(){
if ($(this).parents("table").length < 2) {
$(this).css("display", (idx[loop]==true)? "" : "none"); // hide/show cell
loop++;
}
});
}
else { $("table:eq(0) th, table:eq(0) td").css("display", ""); } // show all cells
}
$(document).ready(function(){
$("#to, #from").keyup(tableFilter);
});
/* ]]> */</script>https://stackoverflow.com/questions/2731921
复制相似问题