循环语句中的全选/取消全选复选框

3

我有一个程序问题。这是我的代码片段。

这是Javascript/Jquery代码。

<script language='javascript'>

///SELECTING CHECKBOXES////
$(function(){

// add multiple select / deselect functionality
$("#selectall").click(function () {
      $('.case').attr('checked', this.checked);
});

// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){

    if($(".case").length == $(".case:checked").length) {
        $("#selectall").attr("checked", "checked");
    } else {
        $("#selectall").removeAttr("checked");
    }

});
});
</script>

这是我将集成该JavaScript代码的位置。

 <h2>Quotation ID</h2>
            <?php

        $select_orders = mysql_query ("SELECT * FROM tblorder WHERE project_id = '$project_id' GROUP BY quotation_id") OR DIE (mysql_error());
        while ($row2=mysql_fetch_array($select_orders)){
            $quote_id = $row2['quotation_id'];


            ?>
        <h3 class="expand"><?php echo $quote_id; ?></h3>
        <div class="collapse">
            <table align='center' border='1' class='display'>
                <thead>
                    <th><input type='checkbox' onclick='checkall()' id='selectall'/></th>
                    <th>Product Type</th>
                    <th width='20px'>Product type code</th>
                    <th width='20px'>Quantity</th>
                    <th>Width</th>
                    <th>Height</th>
                    <th>Total Sub.</th>
                </thead>
                <tbody>
                <?php
                $tots_tots = 0;
                $tots_subs = 0;
                $select_orders2 = mysql_query ("SELECT * FROM tblorder WHERE project_id = '$project_id' AND quotation_id = '$quote_id'") OR DIE (mysql_error());
                    while ($row3=mysql_fetch_array($select_orders2)){
                        $idd = $row3['id'];
                        $project_id2 = $row3['project_id'];
                        $order_id = $row3['quotation_id'];
                        $prod_type = $row3['prod_type'];
                        $prod_type_code = $row3['prod_type_code'];
                        $qty = $row3['qty'];
                        $width = $row3['width'];
                        $height = $row3['height'];
                        $tot_sub = $row3['total_subs'];

                        $tots_subs += $tot_sub;

                echo "<tr bgcolor='".$colors[$c++ % 2]."' align='center'>";
                    echo "<td>
                    <input type='hidden' name='project_name' value='$project_name'>
                    <input type='checkbox' name='check_ptc[]' value='$prod_type_code' style='display:none;' checked>
                    <input type='checkbox' class='case' name='checkbox[]' value='".$idd."'></td>";
                    echo "
                            <input type='hidden' name='project_id[]' value='$project_id2'>
                        </td>";
                    echo "<td>".$prod_type."
                            <input type='hidden' name='quotation_id[]' value='$order_id'>
                            <input type='hidden' name='prod_type[]' value='$prod_type'>
                        </td>";
                    echo "<td>".$prod_type_code."
                            <input type='hidden' name='prod_type_code[]' value='$prod_type_code'>
                        </td>";
                    echo "<td>".$qty."</td>";
                    echo "<td>".$width."</td>";
                    echo "<td>".$height."</td>";
                    echo "<td>".$tot_sub."</td>";
                echo "</tr>";
                }
                echo "<tr>";
                echo "<td></td><td></td><td></td><td></td><td></td>
                        <td>
                            <strong><b>Total:</b></strong>";
                echo "</td>";
                echo "<td>
                            <font color='#900'><u><b>$tots_subs</b></u></font>
                        </td>";

                echo "</tr>";
                ?>
                </tbody>
            </table>
        </div>
        <?php 
        }
        ?>

自从表格处于循环状态,问题就出现在第一个表格出现时。当点击第一个标题复选框时,会选中其他表格中的所有复选框,这不是我想要的。我寻找的方法是是否有一种方式可以迭代复选框的ID和类,或者有其他方法可以实现我想要的功能。
如下图所示,这些3个表格都有自己的复选框标头,我希望它们是各自表格内部的全选按钮。你有什么聪明的想法来解决这个问题呢?
提前感谢。

你可以用更短的代码替换 if($(".case").length == $(".case:checked").length),如下:if(!$(".case:not(:checked)").length) - Joe Frambach
在这个元素中,移除内联的onclick处理程序!jQuery已经为您做了这个!<input type='checkbox' onclick='checkall()' id='selectall'/> - Joe Frambach
3个回答

2
你可以像这样做。
$('.allcb').on('click', function(){
    var childClass = $(this).attr('data-child');
    $('.'+childClass+'').prop('checked', this.checked);
});

FIDDLE

更新

只需将allcb类应用于主复选框,将名为chk的类应用于子复选框即可满足您的需求。这是更新后的FIDDLE


由于我的表格在循环内部,data-child和class allcb也应该迭代。我尝试了这段代码,但它没有起作用:( - user2593560
先生,函数parent() x4有什么用途?抱歉,我是jquery的新手:D为什么你要使用4个parent()?这是因为它被生成了四次吗? - user2593560
1
我使用了parent()x4,因为.allcb被放置在th标签下。所以它的第一个父级将是th,然后thtr的子级,trthead的子级,theadtable的子级。 - Khawer Zeshan
你解释得比文档好多了。 :D - user2593560
是的先生,实际上它没有检查复选框子项。我不知道为什么.. :( div也应该在parent()中指示吗? - user2593560
显示剩余2条评论

1
尝试使用以下PHP代码替代脚本 //检查所有/取消所有复选框。
<a onclick="$(this).parent().find(':checkbox').attr('checked', true);"><?php echo $text_select_all; ?></a>/<a onclick="$(this).parent().find(':checkbox').attr('checked', false);"><?php echo $text_unselect_all; ?></a>

1
使用.prop。
$(function () {
    var $cases = $('.case');

    // add multiple select / deselect functionality
    var $all = $("#selectall").click(function () {
        $$cases.prop('checked', this.checked);
    });

    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $cases.click(function () {
        $all.prop("checked", $cases.filter(":not(:checked)").length) != 0);
    });
});

使用这段代码是必要的..但在我的情况下,这不是我想要的。当我将这段代码集成到我的代码中时..它有相同的问题。当第一个表出现时..并切换checkall复选框,其他表也被选中。这不是我的计划。@Khawer Zeshan的想法类似。但对我来说没有运气。:( - user2593560

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接