【问题标题】:jQuery table column filter with a button带有按钮的 jQuery 表格列过滤器
【发布时间】:2017-09-07 15:42:19
【问题描述】:

我有一个 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>&nbsp;</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>&nbsp;</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>&nbsp;</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>&nbsp;</p>
</td>
<td class="col1"valign="bottom" width="98">
<p>&nbsp;</p>
</td>
<td class="col1"valign="bottom" width="81">
<p>25</p>
</td>
</tr>

【问题讨论】:

    标签: javascript jquery html filtering


    【解决方案1】:

    我会使用.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>&nbsp;</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>&nbsp;</p>
    </td>
    <td class="col1"valign="bottom" width="98">
    <p>&nbsp;</p>
    </td>
    <td class="col1"valign="bottom" width="98">
    <p>&nbsp;</p>
    </td>
    <td class="col1"valign="bottom" width="98">
    <p>&nbsp;</p>
    </td>
    <td class="col1"valign="bottom" width="81">
    <p>25</p>
    </td>
    </tr>

    【讨论】:

    • 效果很好,谢谢!我添加了代码以在隐藏不必要的列后显示列$(function() { $('#butt1').click(function() { $("#tast td.col1:contains('" + $('#selector1').val() + "')").parent().show().siblings().hide(); $("#tast td.col1:contains('" + $('#selector1').val() + "')").parent().show(); }); });
    猜你喜欢
    • 1970-01-01
    • 2013-11-27
    • 2011-03-28
    • 2015-02-26
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多