我有一个 HTML 表格,按下按钮时应通过下拉列表中的选定值进行过滤。
我使用 jQuery 选择器 #table_id td.[td class]:contains
和 #table_id td.[td class]:not(:contains)
。一切看起来都很好,但 :not(:contains)
选择器返回“语法错误,无法识别的表达式”。它在其他示例中以相同的方式工作,但由于某种原因无法使其工作。
$(function() {
$('#butt1').click(function() {
$("#tast td.col1:contains('" + $('#selector1').val() + "')").parent().show();
$("#tast td.col1:not(:contains('" + $('#selector1').val() + "'))").parent().hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<select id="selector1"><option value="all">All</option><option value="
Sample 1
">
Sample 1
</option></select>
<button id="butt1">
Filter
</button>
<table id="tast" class="TableStyle0">
<thead>
<tr>
<th width="90">
<p style="text-align: center;"><strong>Code</strong></p>
</th>
<th style="text-align: center;" width="203">
<p><strong>Dir</strong></p>
</th>
<th style="text-align: center;" width="136">
<p><strong>Inst</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Form</strong></p>
</th>
<th style="text-align: center;" width="91">
<p><strong>Quota</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Target</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Total</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Total payed</strong></p>
</th>
<th style="text-align: center;" width="81">
<p><strong>Told</strong></p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col1" valign="bottom" width="90">
<p>38.03.02</p>
</td>
<td class="col1"valign="bottom" width="203">
<p>Sample 1</p>
</td>
<td class="col1"valign="bottom" width="136">
<p>Data 1</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>Text 1</p>
</td>
<td class="col1"valign="bottom" width="91">
<p>1</p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p>4</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>5</p>
</td>
<td class="col1"valign="bottom" width="81">
<p>60</p>
</td>
</tr>
<tr>
<td class="col1"valign="bottom" width="90">
<p>38.03.02</p>
</td>
<td class="col1"valign="bottom" width="203">
<p>Sample 2</p>
</td>
<td class="col1"valign="bottom" width="136">
<p>Data 2</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>Text 3</p>
</td>
<td class="col1"valign="bottom" width="91">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="81">
<p>25</p>
</td>
</tr>
最佳答案
我将使用 .siblings()
选择所有无效元素,然后调用 .hide()
而不是 :not(:contains() )
,因为它更直观,并且会减少重复代码。
$(function() {
$('#butt1').click(function() {
$("#tast td.col1:contains('" + $('#selector1').val() + "')").parent().show().siblings().hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<select id="selector1"><option value="all">All</option><option value="
Sample 1
">
Sample 1
</option></select>
<button id="butt1">
Filter
</button>
<table id="tast" class="TableStyle0">
<thead>
<tr>
<th width="90">
<p style="text-align: center;"><strong>Code</strong></p>
</th>
<th style="text-align: center;" width="203">
<p><strong>Dir</strong></p>
</th>
<th style="text-align: center;" width="136">
<p><strong>Inst</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Form</strong></p>
</th>
<th style="text-align: center;" width="91">
<p><strong>Quota</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Target</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Total</strong></p>
</th>
<th style="text-align: center;" width="98">
<p><strong>Total payed</strong></p>
</th>
<th style="text-align: center;" width="81">
<p><strong>Told</strong></p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col1" valign="bottom" width="90">
<p>38.03.02</p>
</td>
<td class="col1"valign="bottom" width="203">
<p>Sample 1</p>
</td>
<td class="col1"valign="bottom" width="136">
<p>Data 1</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>Text 1</p>
</td>
<td class="col1"valign="bottom" width="91">
<p>1</p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p>4</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>5</p>
</td>
<td class="col1"valign="bottom" width="81">
<p>60</p>
</td>
</tr>
<tr>
<td class="col1"valign="bottom" width="90">
<p>38.03.02</p>
</td>
<td class="col1"valign="bottom" width="203">
<p>Sample 2</p>
</td>
<td class="col1"valign="bottom" width="136">
<p>Data 2</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>Text 3</p>
</td>
<td class="col1"valign="bottom" width="91">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="98">
<p> </p>
</td>
<td class="col1"valign="bottom" width="81">
<p>25</p>
</td>
</tr>
关于javascript - 带按钮的 jQuery 表列过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43359712/