简体   繁体   English

计算多个面板中选中的复选框的数量,并使用 jQuery/JavaScript 在正确的面板中显示相关数字

[英]Count the number of checked check-boxes in multiple panels and show the relevant number in the correct panel using jQuery/JavaScript

I have multiple panels (class="panel-section) and I was to count the number of checked check-boxes in each and display this number in each panel, and have this number update when there is a check-box change. This is my HTML code:我有多个面板 (class="panel-section),我要计算每个面板中选中的复选框的数量,并在每个面板中显示这个数字,并在复选框发生变化时更新这个数字。这是我的 HTML 代码:

<div class="panel-section">
    <!-- Layer Group Heading -->
    <div class="panel-section-top">
        <a href="javascript:;" class="expand-toggle">Group 1
        <span id="layer_group_1_count_checkboxes" class="badge"></span></a>
    </div> <!-- end (Group Name) -->    
    <div class="panel-section-main" id="layer_group_1"> 
        <!-- Layer -->
        <div class="layer">
            <div class="list-group" style="margin-bottom: 0px;">
                <label class="checkbox-inline switch-button" for="visible10">
                    <input type="checkbox" name="layer_10" class="visible" id="visible10">
                    <div class="switch-button-background">
                        <div class="switch-button-button"></div>
                    </div>
                </label>
                <h4>Layer 1</h4>
            </div>
        </div> <!-- end .layer -->
        <!-- Layer -->
        <div class="layer">
            <div class="list-group" style="margin-bottom: 0px;">
                <label class="checkbox-inline switch-button" for="visible11">
                    <input type="checkbox" name="layer_11" class="visible" id="visible11">
                    <div class="switch-button-background">
                        <div class="switch-button-button"></div>
                    </div>
                </label>
                <h4>Layer 2</h4>
            </div>
        </div> <!-- end .layer -->
    </div> <!-- end .layer-main -->
</div> <!-- end .panel-section layer_group_1 -->
<div class="panel-section">
    <!-- Layer Group Heading -->
    <div class="panel-section-top">
        <a href="javascript:;" class="expand-toggle">Group 2
        <span id="layer_group_2_count_checkboxes" class="badge"></span></a>
    </div> <!-- end (Group Name) -->    
    <div class="panel-section-main" id="layer_group_2"> 
        <!-- Layer -->
        <div class="layer">
            <div class="list-group" style="margin-bottom: 0px;">
                <label class="checkbox-inline switch-button" for="visible20">
                    <input type="checkbox" name="layer_20" class="visible" id="visible20">
                    <div class="switch-button-background">
                        <div class="switch-button-button"></div>
                    </div>
                </label>
                <h4>Layer 1</h4>
            </div>
        </div> <!-- end .layer -->
        <!-- Layer -->
        <div class="layer">
            <div class="list-group" style="margin-bottom: 0px;">
                <label class="checkbox-inline switch-button" for="visible11">
                    <input type="checkbox" name="layer_21" class="visible" id="visible21">
                    <div class="switch-button-background">
                        <div class="switch-button-button"></div>
                    </div>
                </label>
                <h4>Layer 2</h4>
            </div>
        </div> <!-- end .layer -->
    </div> <!-- end .layer-main -->
</div> <!-- end .panel-section layer_group_2 -->

And I can get the number of total check-boxes that are active using the following:我可以使用以下方法获取活动复选框的总数:

    //Get the number of total layers active
    $(document).ready(function () {
      $("input[type=checkbox]").each(function () {
        $(this).change(updateCount);
      });
      updateCount();
      function updateCount () {
        var count = $("input[type=checkbox]:checked").size();
        $("#count_checkboxes").text(count);
        $("#status").toggle(count > 0);
      };
    });

But instead of just "#count_checkboxes" I want the id from the panel-section-main div to join with it so I can pass the number to the right section (eg "#layer_group_1_count_checkboxes").但不仅仅是“#count_checkboxes”,我希望 panel-section-main div 中的 id 与其连接,这样我就可以将数字传递到正确的部分(例如“#layer_group_1_count_checkboxes”)。

I may not be going about this the best way so also open to better ideas people may have.我可能不会以最好的方式解决这个问题,所以也愿意接受人们可能有的更好的想法。 This is an example of the output I want:这是我想要的输出示例:

在此处输入图像描述

When trying to genericise content, aka.当试图通用化内容时,又名。 DRY (Don't Repeat Yourself) it up, you should avoid id attributes as they are the opposite of what you need. DRY(不要重复自己)它,你应该避免id属性,因为它们与你需要的相反。 Instead target elements by their classes so you can deal with them by their behaviour instead of targeting them directly.而是通过类来定位元素,这样您就可以通过它们的行为来处理它们,而不是直接以它们为目标。

As such you can achieve what you need when a checkbox is changed by looping through all the panels and finding the number of checked checkboxes and updating the .badge within that panel.因此,通过遍历所有面板并查找选中的复选框的数量并更新该面板中的.badge ,您可以在更改复选框时实现所需的功能。

Also note that size() has been removed from the latest versions of jQuery.另请注意, size()已从最新版本的 jQuery 中删除。 I'd suggest updating to jQuery 3.4 and using the length property instead.我建议更新到 jQuery 3.4 并改为使用length属性。 Try this:试试这个:

 jQuery(function($) { $("input[type=checkbox]").on('change', updateCount); updateCount(); function updateCount() { $('.panel-section').each(function() { var $panel = $(this); var count = $panel.find(':checkbox:checked').length; $panel.find('.badge').text(count); }); }; });
 .panel-section-main { border: 1px solid #CCC; } a { text-decoration: none; }.badge { background-color: #C00; border-radius: 50%; padding: 5px 9px; color: #FFF; }.panel-section-main.layer.list-group { margin-bottom: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <div class="panel-section"> <div class="panel-section-top"> <a href="#" class="expand-toggle"> Group 1 <span class="badge"></span> </a> </div> <div class="panel-section-main"> <div class="layer"> <div class="list-group"> <label class="checkbox-inline switch-button"> <input type="checkbox" name="layer_10" class="visible" id="visible10"> <div class="switch-button-background"> <div class="switch-button-button"></div> </div> </label> <h4>Layer 1</h4> </div> </div> <div class="layer"> <div class="list-group"> <label class="checkbox-inline switch-button"> <input type="checkbox" name="layer_11" class="visible" id="visible11"> <div class="switch-button-background"> <div class="switch-button-button"></div> </div> </label> <h4>Layer 2</h4> </div> </div> </div> </div> <div class="panel-section"> <div class="panel-section-top"> <a href="#" class="expand-toggle"> Group 2 <span class="badge"></span> </a> </div> <div class="panel-section-main"> <div class="layer"> <div class="list-group"> <label class="checkbox-inline switch-button" for="visible20"> <input type="checkbox" name="layer_20" class="visible" id="visible20"> <div class="switch-button-background"> <div class="switch-button-button"></div> </div> </label> <h4>Layer 1</h4> </div> </div> <div class="layer"> <div class="list-group"> <label class="checkbox-inline switch-button"> <input type="checkbox" name="layer_21" class="visible" id="visible21"> <div class="switch-button-background"> <div class="switch-button-button"></div> </div> </label> <h4>Layer 2</h4> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM