我有一个表,我希望有一个按钮onClick,它将运行toggle()函数
现在,我可以把M值隐藏起来,但不能把它们带回来,
比如说,这就是表的样子。
+-------+--------+
| Name | Gender |
+-------+--------+
| James | M |
+-------+--------+
| Gemma | F |
+-------+--------+
| John | F |
+-------+--------+这是切换函数
function toggle() {
var searchString = 'M';
$("#t01 tr td:contains('" + searchString + "')").each(function() {
if ($(this).text() == searchString) {
$(this).parent().hide();
}
});
}发布于 2016-02-05 13:26:43
从tr到内部检查会更容易,如下所示:
function toggle(gender) {
$('#t01 tbody tr').each(function() {
$(this).toggle($('td:eq(1):contains(' + gender + ')', this).length > 0);
})
}
function show() {
$('#t01 tbody tr').show();
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="t01">
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr>
<td>James</td>
<td>M</td>
</tr>
<tr>
<td>Gemma</td>
<td>F</td>
</tr>
<tr>
<td>John</td>
<td>F</td>
</tr>
</tbody>
</table>
<button onclick="toggle('M')">Male</button>
<button onclick="toggle('F')">Female</button>
<button onclick="show()">All</button>
发布于 2016-02-05 13:33:15
如果您的代码在隐藏行方面工作正常,那么您可以简单地用toggle()替换hide(),以显示/隐藏其余的代码,或者可以像BG101所说的那样简化代码。
function toggle() {
var searchString = 'M';
$("#t01 tr td:contains('" + searchString + "')").each(function() {
if ($(this).text() == searchString) {
$(this).parent().toggle();
}
});
}https://stackoverflow.com/questions/35224955
复制相似问题