【问题标题】:jQuery - On click, check all checkboxes in class, if checked (do this) if not (then)jQuery - 单击时,选中类中的所有复选框,如果选中(执行此操作),如果没有(则)
【发布时间】:2016-11-13 12:21:55
【问题描述】:

我有一个由 MySQL 查询填充的 PHP 页面。我现在想根据选中的复选框过滤表格结果。

当页面加载时,所有结果都会显示。然后在更改复选框时,结果将根据选中的复选框过滤、隐藏或显示行!

有人可以帮我处理 jQuery 代码吗?我有;

$(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()
    });
}); 

有人可以解释我如何编辑此代码,因此如果选中复选框,则会显示表中的结果( $(this.value).parents('tr').show() ),或者,如果复选框未选中,则 .hide(),但如果未选中所有复选框,则显示所有结果..

复选框由 PHP 代码创建,

        $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>"; 
    }

这将从表中获取供应商类型,并根据返回值创建一个复选框。然后将复选框的值设置为返回值,小写并用“_”替换空格。 然后用以下内容填充表格行和表格数据字段

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

这会使用带有“filter_”前缀的值来设置 TD 的类 然后,当您选中一个复选框时,它可以获取该值,为其添加前缀 'filter_' 并显示或隐藏该类型的所有行。

接受任何解决此问题的建议。

【问题讨论】:

    标签: php jquery mysql


    【解决方案1】:

    类似的东西

    $(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>

    【讨论】:

    • 完美!!!非常感谢!你能解释一下'$('[class^="filter_"]')。'做?我对 jQuery 非常陌生。
    • 它选择所有具有以filter_开头的类属性的元素
    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 2015-02-02
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多