duanou9739 2016-11-13 12:21
浏览 41
已采纳

jQuery - 点击后,检查课堂上的所有复选框,如果选中(如果没有)(那么)

I have a PHP page that is populated by a MySQL query. I now want to filter the table results based on checkboxes that are selected.

When the page is loaded all results show. Then on change of a checkbox, the results will filter, hiding or showing rows based on what checkboxes are checked!

Can someone please help me with the jQuery code. I have;

$(function() {
$(".filterCheck").change(function() {
    var checkArr = $('.filterCheck:checked').map(function() {
        $ = '.filter_' + this.value;
        $($).parents('tr').show();
        return this.value;            
    }).get();
    $.each(checkArr, function(i, val) {
        alert('No values');
        $v = ".filter_" + val;
        $($v).parents('tr').show()
    });
}); 

Can someone explain how I can edit this code, so if the checkbox is selected the results in the table are shown ( $(this.value).parents('tr').show() ) or, if the checkbox is NOT selected, then .hide(), BUT if ALL checkboxes are unchecked, then ALL results are shown..

The checkboxes are created by the PHP code,

        $db_results = mysqli_query(Database::$conn, "SELECT * FROM suppliers WHERE ten_ID = $tenid ORDER BY sup_Name;");

    /* make an array from all the suplier types 
     * build cheklist of the types */
    echo "<br><h5 class='text-muted'>Filter by supplier type</h5>";
    $db_types = [];
    while ($row = mysqli_fetch_array($db_results)) { array_push($db_types, $row['sup_Type']); }
    sort($db_types);
    $db_types = array_unique($db_types);
    foreach ($db_types as $i) {
        echo "<div class='checkbox'>";
        $t = strtolower(str_replace(' ', '_', $i));
        echo "<label><input type='checkbox' class='filterCheck' value='$t'>$i</label>";
        echo "</div>"; 
    }

This will get the supplier type from the table, and create a checkbox from the returned value. The value of the checkbox is then set as the returned value, in lower case and replacing white space with '_'. The table row, and table data fields are then populated with the below

            $t = strtolower(str_replace(' ', '_', $row['sup_Type']));
        echo "<td class='filter_$t'>" . $row['sup_Type'] . "</td>";

This sets the class of the TD with the value prefixed with 'filter_' Then when you check a checkbox, it can take the value, prefix it with 'filter_' and show or hide all rows with that type.

Open to ANY suggestions to fix this.

  • 写回答

1条回答 默认 最新

  • doudan5136 2016-11-13 12:36
    关注

    Something like this

    $(function() {
        $(".filterCheck").on('change', function() {
            var checked = $('.filterCheck:checked'),
                trs     = $('[class^="filter_"]').closest('tr');
    
            if (checked.length === 0) { // no boxes checked
                trs.show();
            } else {
                trs.hide();
                checked.each(function() {
                    $('.filter_' + this.value).closest('tr').show();
                });
            }
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <h5 class='text-muted'>Filter by supplier type</h5>
    <div class='checkbox'>
        <label><input type='checkbox' class='filterCheck' value='test1'>Test 1</label>
        <label><input type='checkbox' class='filterCheck' value='test2'>Test 2</label>
        <label><input type='checkbox' class='filterCheck' value='test3'>Test 3</label>
    </div>
    <br /><br />
    <table>
        <tr><td class='filter_test1'>test1</td></tr>
        <tr><td class='filter_test2'>test2</td></tr>
        <tr><td class='filter_test3'>test3</td></tr>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作