简体   繁体   English

如何根据选中的复选框数量显示/隐藏元素?

[英]How to show/hide elements based on number of checkbox checked?

I want to show all the buttons listed in class when number of checked boxes becomes 1. By default I am showing only add folder and upload. 当复选框的数量变为1时,我想显示班级中列出的所有按钮。默认情况下,我仅显示添加文件夹和上传。

When a user selects 2 checkboxes, the share button should be hidden, and when it unchecks it, it should again be visible. 当用户选中2个复选框时,共享按钮应该被隐藏,而当取消选中它时,它应该再次可见。

Both the things are working fine according to my code but the problem arises when user checks more than 2 checkboxes , it should work when two checkboxes are selected. 根据我的代码,这两件事都可以正常工作,但是当用户选中两个以上的复选框时会出现问题,当选中两个复选框时它应该可以工作。

HTML-code: HTML代码:

<ul class="head-btn">
                <li><a href="" class="addfolder"><i class=" fa fa-plus icon-bgs"></i>Add Folder</a>  </li>
                <li ><a href="" class="upload"><i class=" fa fa-cloud-upload icon-bgs"></i>Upload</a>  </li>
                <li ><a href="" class="share"><i class=" fa fa-user icon-bgs"></i>Share</a>  </li>
                <li ><a href="" class="download"><i class=" fa fa-cloud-download icon-bgs"></i>Download</a>  </li>
            </ul>

<div class="doclist-cont">

    <div class="list-group">

        <div class="list-group-item">
            <div class="checkbox"><label><input type="checkbox"></label></div>
            <i class="fa fa-folder-open-o folder"></i>
            <span class="name" >Introduction Document </span>
            <span class="left-more-icon"></span>
        </div>


        <div class="list-group-item">
            <div class="checkbox"><label><input type="checkbox"></label></div>
            <i class="fa fa-folder-open-o folder"></i>
            <span class="name" >Platform details document</span>
            <span class="right-more-icon"></span>

        </div>
        <div class="list-group-item">
            <div class="checkbox"><label><input type="checkbox"></label></div>
            <i class="fa fa-file-word-o word"></i>
            <span class="name" >Station list.docx</span>
            <span class="text-muted">Jul 21, 2015  |  25 KB</span>
            <span class="right-more-icon"></span>
        </div>
        <div class="list-group-item">
            <div class="checkbox"><label><input type="checkbox"></label></div>
            <i class="fa  fa-file-pdf-o pdf-icon"></i>
            <span class="name" >Platform details document</span>
            <span class="text-muted">Jul 21, 2015  |  25 KB</span>
            <span class="right-more-icon"></span>
        </div>
    </div>

here is my jquery code: 这是我的jQuery代码:

 $(document).ready(function() {

            $('.share').hide();
            $('.download').hide();
            var counter=0;
            var totalCheckboxes = $('input:checkbox').length;
            $('input[type="checkbox"]').click(function(){
                var numberOfChecked = $('input:checkbox:checked').length;
                if(($(this).prop("checked") == true && numberOfChecked ==1))
                {
                    $('.share').show();
                    $('.download').show();


                }
                else if($(this).prop("checked") == false && numberOfChecked !=1)
                {
                    $('.share').hide();
                    $('.download').hide();
                }
                else if(($(this).prop("checked") == true && numberOfChecked >1)){
                    $(".share").hide();

                }

                else if(($(this).prop("checked") == false && numberOfChecked ==1)){
                    $(".share").show();
                }


            });
        });

I have simplified your code a bit. 我已经简化了您的代码。 I think it meets all your conditions now and it's bit easier to read (although I'm not sure what do you mean by "it should work when two checkboxes are selected" as it's not clear what do you mean). 我认为它现在已经满足您的所有条件,并且更容易阅读(尽管我不确定您的意思是“当选中两个复选框时它应该工作”,因为您不清楚这是什么意思)。

Here are the conditional statements: 以下是条件语句:

     if (numberOfChecked == 0) {
         $('.share').hide();
         $('.download').hide();
     } else if (numberOfChecked == 1) {
         $('.share').show();
         $('.download').show();
     } else if (numberOfChecked >= 2) {
         $(".share").hide();
         $('.download').show(); //not really needed but added for clarity 
     }

and a FIDDLE : https://jsfiddle.net/82rdkkdh/1/ 和一个FIDDLEhttps : //jsfiddle.net/82rdkkdh/1/

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

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