Limit Checkbox amount

前端 未结 6 797
孤独总比滥情好
孤独总比滥情好 2020-11-29 05:35

I have 20 Checkboxes. I need to disable 16 Checkboxes, if 4 checkboxes are selected.

I tryed this begann with this jquery code

    $(\"input[type=ch         


        
相关标签:
6条回答
  • 2020-11-29 05:45

    Here is a working example. May be it would be usefull for some people ;-)

    $.fn.limit = function(n) {
     var self = this;
     this.click(function(){
       (self.filter(":checked").length==n)?
         self.not(":checked").attr("disabled",true).addClass("alt"):
         self.not(":checked").attr("disabled",false).removeClass("alt");
     });
    }
    
    $("input:checkbox").limit(3);
    
    }
    
    0 讨论(0)
  • 2020-11-29 05:54

    My solution:

    // Function to check and disable checkbox
        function limit_checked( element, size ) {
          var bol = $( element + ':checked').length >= size;
          $(element).not(':checked').attr('disabled',bol);
        }
    
        // List of checkbox groups to check + number of checked alowed
        var check_elements = [
          { id: '.group1 input[type=checkbox]', size: 2 },
          { id: '.group2 input[type=checkbox]', size: 3 },
        ];
    
        // Run function for each group in list
        $(check_elements).each( function(index, element) {
          // Limit checked on window load
          $(window).load( function() {
            limit_checked( element.id, element.size );
          })
    
          // Limit checked on click
          $(element.id).click(function() {
            limit_checked( element.id, element.size );
          });
        });
    
    0 讨论(0)
  • 2020-11-29 06:03

    Providing a parent element with an id of limit_checkbox will help speed up the selection, caching the query reduces the amount of DOM interaction which is slow.

    Used "change" instead of click otherwise the code could be circumvented by using the keyboard, also using live instead of bind which is a lot quicker.

    // Cache jQuery selection
    var $checkboxes_to_limit = $("#limit_checkbox").find("input:checkbox[name=cate]");
    $checkboxes_to_limit.live("change", function() {
        if($checkboxes_to_limit.filter(":checked").length >= 4) {
            $checkboxes_to_limit.not(":checked").attr("disabled","disabled");
        }
        else {
            $checkboxes_to_limit.removeAttr("disabled");
        }
    });
    
    0 讨论(0)
  • 2020-11-29 06:06
    $("input[type=checkbox][name=cate]").click(function() {
    
        var bol = $("input[type=checkbox][name=cate]:checked").length >= 4;     
        $("input[type=checkbox][name=cate]").not(":checked").attr("disabled",bol);
    
    });
    

    demo

    0 讨论(0)
  • 2020-11-29 06:06

    I found a more flexible solution that lets you vary the limit for different sets of checkboxes.

    // Assign a class of limit_checkbox to the containing DIV and
    // set a data-limit attribute with your limit. 
    $(".limit_checkbox").on("change", function()
    {
      var limit = $(this).attr('data-limit'),
          checkboxes = $(this).find("input:checkbox"),
          valid = checkboxes.filter(":checked").length >= limit;
    
        checkboxes.not(":checked").attr("disabled", valid);
    });
    

    The only problem I found was that if your validation fails and returns the page with the input it will let the user select one extra box before disabling the rest. The way I solved this was to count how many were checked and disable the rest on the server before sending the page with input back.

    hope it helps Cheers!

    0 讨论(0)
  • 2020-11-29 06:07

    This does not DISABLE the remaining checkboxes, but prevent from selecting more. I will share anyway, here it is:

    .aspx

    <asp:CheckBoxList id="chkList" runat="server" RepeatLayout="Flow" />
    

    .js

    $(document).ready(function () {
        LimitCheckboxes('input[name*=chkList]', 3);
    }
    
    
    
    function LimitCheckboxes(control, max) {
    
        $(control).live('click', function () {
    
            //Count the Total Selection in CheckBoxList 
            var totCount = 1;
            $(this).siblings().each(function () {
                if ($(this).attr("checked")) { totCount++; }
            });
    
            //if number of selected item is greater than the max, dont select.
            if (totCount > max) { return false; }
            return true;
        });
    
    }
    

    PS: Make sure you use the RepeatLayout="Flow" to get rid of the annoying table format.

    0 讨论(0)
提交回复
热议问题