简体   繁体   English

自定义复选框选择限制不起作用

[英]Custom checkbox selection limit is not working

Since i have designed the custom checkbox, i wanted to limit the checkbox. 由于我已经设计了自定义复选框,因此我想限制该复选框。 I could easily add limits for default checkbox but in my case its not working because of my own design & I have no idea about custom checkbox selection limits. 我可以轻松添加默认复选框的限制,但是由于我自己的设计,我无法使用默认复选框,而且我不知道自定义复选框选择的限制。 Please help me to get out off it. 请帮我摆脱它。

 $(document).ready(function(){ $('.btcp-multi-lst').on('change', function(evt) { if ($('input').is(':checked').length >= 3) { this.checked = false; }; }); }); 
 .checkbox{ padding-left: 20px; } .checkbox>label { display: inline-block; position: relative; padding-left: 5px; color: #686868; padding-top: 2px; min-height: 25px; font-family: 'montserrat'; } .checkbox>span{ padding-right: 17px; padding-top: 3px; font-family: 'montserrat'; } .checkbox>label::before { content: ""; display: inline-block; position: absolute; width: 17px; height: 17px; left: 0; top:4px; margin-left: -20px; border: 1px solid #cccccc; border-radius: 3px; /*background-color: #ccc;*/ -webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out; -o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out; transition: border 0.15s ease-in-out, color 0.15s ease-in-out; /*z-index:25156332;*/ } .checkbox>label::after { display: inline-block; position: absolute; width: 16px; height: 16px; left: 0; top: 4px; margin-left: -20px; padding-left: 3px; padding-top: 1px; font-size: 11px; color: #555555; /*z-index:25156332;*/ } .checkbox input[type="checkbox"] { opacity: 0; } .checkbox input[type="checkbox"]:focus + label::before { outline: thin dotted; outline: 5px auto -webkit-focus-ring-color; outline-offset: -2px; } .checkbox input[type="checkbox"]:checked + label{ color: #1E6C97; } .checkbox input[type="checkbox"]:checked + label::after { font-family: 'FontAwesome'; content: "\\f00c"; } .checkbox input[type="checkbox"]:disabled + label { opacity: 0.65; } .checkbox input[type="checkbox"]:disabled + label::before { background-color: #eeeeee; cursor: not-allowed; } .checkbox.checkbox-circle label::before { border-radius: 50%; } .checkbox.checkbox-inline { margin-top: 0; } .fltr-chk-box-bg{ background-color: #E3E7EA; color: #1E6C97; } .checkbox-primary input[type="checkbox"]:checked + label::before { background-color: #1E6C97; border-color: #428bca; } .checkbox-primary input[type="checkbox"]:checked + label::after { color: #fff; } .checkbox input[type="checkbox"]:checked + label + span{ position: relative; z-index: 75456; } .checkbox input[type="checkbox"]:checked + label + span:before{ content: ''; position: absolute; top: 0; right: 0; border-bottom: 13px solid #CCD2D6; border-top: 11px solid #CCD2D6; border-right: 50px solid #CCD2D6; border-left: 6px solid transparent; z-index: -7523; } 
 <script src="https://use.fontawesome.com/aacdcb9275.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="well btcp-multi-lst bg-white margin-btm-0" id="scrollbar"> <form> <div class="checkbox checkbox-primary"> <input id="category1" type="checkbox" value=""> <label for="category1">Manufacturer</label> </div> <div class="checkbox checkbox-primary"> <input id="category2" type="checkbox" value=""> <label for="category2">Trading Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category3" type="checkbox" value=""> <label for="category3">Distributor / Merchant / Stockist</label> </div> <div class="checkbox checkbox-primary"> <input id="category4" type="checkbox" value=""> <label for="category4">Wholesaler / Dealer</label> </div> <div class="checkbox checkbox-primary"> <input id="category5" type="checkbox" value=""> <label for="category5">Exporter</label> </div> <div class="checkbox checkbox-primary"> <input id="category6" type="checkbox" value=""> <label for="category6">Converter</label> </div> <div class="checkbox checkbox-primary"> <input id="category7" type="checkbox" value=""> <label for="category7">Broker</label> </div> <div class="checkbox checkbox-primary"> <input id="category8" type="checkbox" value=""> <label for="category8">Direct Seller</label> </div> <div class="checkbox checkbox-primary"> <input id="category9" type="checkbox" value=""> <label for="category9">Retailer</label> </div> <div class="checkbox checkbox-primary"> <input id="category10" type="checkbox" value=""> <label for="category10">Indenting Agent / Buying Office / Purchasing Agent</label> </div> <div class="checkbox checkbox-primary"> <input id="category11" type="checkbox" value=""> <label for="category11">Carrying & Forwarding Agent</label> </div> <div class="checkbox checkbox-primary"> <input id="category12" type="checkbox" value=""> <label for="category12">Packers</label> </div> <div class="checkbox checkbox-primary"> <input id="category13" type="checkbox" value=""> <label for="category13">Printers</label> </div> <div class="checkbox checkbox-primary"> <input id="category14" type="checkbox" value=""> <label for="category14">Recycling Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category15" type="checkbox" value=""> <label for="category15">Service Provider</label> </div> <div class="checkbox checkbox-primary"> <input id="category16" type="checkbox" value=""> <label for="category16">Warehousing Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category17" type="checkbox" value=""> <label for="category17">Logistics Provider</label> </div> <div class="checkbox checkbox-primary"> <input id="category18" type="checkbox" value=""> <label for="category18">Mining Company</label> </div> </form> </div> 

is(':checked') function returns a single, boolean value, which on you can't use length property. is(':checked')函数返回一个布尔值,您不能使用length属性。

Suggested approach would be selecting all the checked checkboxes with $('input[type="checkbox"]:checked').length > 1) selector. 建议的方法是使用$('input[type="checkbox"]:checked').length > 1)选择器选择所有选中的复选框。 If their amount is bigger than eg 1 , disable other checkboxes. 如果它们的数量大于例如1 ,则禁用其他复选框。

 $(document).ready(function(){ $('.btcp-multi-lst').on('change', function(e) { if ($('input[type="checkbox"]:checked').length > 1) { $('input[type="checkbox"]:not(:checked)').prop('disabled', true); } else { $('input[type="checkbox"]:not(:checked)').prop('disabled', false); } }); }); 
 .checkbox{ padding-left: 20px; } .checkbox>label { display: inline-block; position: relative; padding-left: 5px; color: #686868; padding-top: 2px; min-height: 25px; font-family: 'montserrat'; } .checkbox>span{ padding-right: 17px; padding-top: 3px; font-family: 'montserrat'; } .checkbox>label::before { content: ""; display: inline-block; position: absolute; width: 17px; height: 17px; left: 0; top:4px; margin-left: -20px; border: 1px solid #cccccc; border-radius: 3px; /*background-color: #ccc;*/ -webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out; -o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out; transition: border 0.15s ease-in-out, color 0.15s ease-in-out; /*z-index:25156332;*/ } .checkbox>label::after { display: inline-block; position: absolute; width: 16px; height: 16px; left: 0; top: 4px; margin-left: -20px; padding-left: 3px; padding-top: 1px; font-size: 11px; color: #555555; /*z-index:25156332;*/ } .checkbox input[type="checkbox"] { opacity: 0; } .checkbox input[type="checkbox"]:focus + label::before { outline: thin dotted; outline: 5px auto -webkit-focus-ring-color; outline-offset: -2px; } .checkbox input[type="checkbox"]:checked + label{ color: #1E6C97; } .checkbox input[type="checkbox"]:checked + label::after { font-family: 'FontAwesome'; content: "\\f00c"; } .checkbox input[type="checkbox"]:disabled + label { opacity: 0.65; } .checkbox input[type="checkbox"]:disabled + label::before { background-color: #eeeeee; cursor: not-allowed; } .checkbox.checkbox-circle label::before { border-radius: 50%; } .checkbox.checkbox-inline { margin-top: 0; } .fltr-chk-box-bg{ background-color: #E3E7EA; color: #1E6C97; } .checkbox-primary input[type="checkbox"]:checked + label::before { background-color: #1E6C97; border-color: #428bca; } .checkbox-primary input[type="checkbox"]:checked + label::after { color: #fff; } .checkbox input[type="checkbox"]:checked + label + span{ position: relative; z-index: 75456; } .checkbox input[type="checkbox"]:checked + label + span:before{ content: ''; position: absolute; top: 0; right: 0; border-bottom: 13px solid #CCD2D6; border-top: 11px solid #CCD2D6; border-right: 50px solid #CCD2D6; border-left: 6px solid transparent; z-index: -7523; } 
 <script src="https://use.fontawesome.com/aacdcb9275.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="well btcp-multi-lst bg-white margin-btm-0" id="scrollbar"> <form> <div class="checkbox checkbox-primary"> <input id="category1" type="checkbox" value=""> <label for="category1">Manufacturer</label> </div> <div class="checkbox checkbox-primary"> <input id="category2" type="checkbox" value=""> <label for="category2">Trading Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category3" type="checkbox" value=""> <label for="category3">Distributor / Merchant / Stockist</label> </div> <div class="checkbox checkbox-primary"> <input id="category4" type="checkbox" value=""> <label for="category4">Wholesaler / Dealer</label> </div> <div class="checkbox checkbox-primary"> <input id="category5" type="checkbox" value=""> <label for="category5">Exporter</label> </div> <div class="checkbox checkbox-primary"> <input id="category6" type="checkbox" value=""> <label for="category6">Converter</label> </div> <div class="checkbox checkbox-primary"> <input id="category7" type="checkbox" value=""> <label for="category7">Broker</label> </div> <div class="checkbox checkbox-primary"> <input id="category8" type="checkbox" value=""> <label for="category8">Direct Seller</label> </div> <div class="checkbox checkbox-primary"> <input id="category9" type="checkbox" value=""> <label for="category9">Retailer</label> </div> <div class="checkbox checkbox-primary"> <input id="category10" type="checkbox" value=""> <label for="category10">Indenting Agent / Buying Office / Purchasing Agent</label> </div> <div class="checkbox checkbox-primary"> <input id="category11" type="checkbox" value=""> <label for="category11">Carrying & Forwarding Agent</label> </div> <div class="checkbox checkbox-primary"> <input id="category12" type="checkbox" value=""> <label for="category12">Packers</label> </div> <div class="checkbox checkbox-primary"> <input id="category13" type="checkbox" value=""> <label for="category13">Printers</label> </div> <div class="checkbox checkbox-primary"> <input id="category14" type="checkbox" value=""> <label for="category14">Recycling Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category15" type="checkbox" value=""> <label for="category15">Service Provider</label> </div> <div class="checkbox checkbox-primary"> <input id="category16" type="checkbox" value=""> <label for="category16">Warehousing Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category17" type="checkbox" value=""> <label for="category17">Logistics Provider</label> </div> <div class="checkbox checkbox-primary"> <input id="category18" type="checkbox" value=""> <label for="category18">Mining Company</label> </div> </form> </div> 

I did a few changes to your selectors because they were a bit off, also you have to use evt.target in order to actually pinpoint which input is being targeted in order to set your limit. 我对选择器进行了一些更改,因为它们有些偏离,您还必须使用evt.target才能真正查明要定位的输入,以便设置限制。 If you want a maximum of 2 checked boxes, then change the > to >= just like in your original code, if you want a maximum of 3 leave it just like that. 如果您最多需要2个复选框,则将>更改为>= ,就像在原始代码中一样,如果您希望最多3个,则将其保留为原来的样子。

$('.btcp-multi-lst').on('click', function(evt) {
    if ($(this).find('input:checked').length > 3) {
      $(evt.target).prop("checked", false);
    };
});

Working Fiddle 工作小提琴

Cheers 干杯

Your selection of checked ones is wrong. 您选择的checked是错误的。

Firstly you should change the selection for binding change event: 首先,您应该更改绑定change事件的选择:

$('input[type=checkbox]').on('change', function(evt) {

And then change the selection for checked ones: 然后更改checked的选择:

$('form input[type=checkbox]:checked').length

Last, you should compare with your limit plus 1. Because when you check a checkbox, the length of checked ones is now (limit + 1). 最后,您应该将限制与加1进行比较。因为当您选中一个复选框时,被选中的复选框的长度为(限制+ 1)。 After setting this.checked = false , the length is limit now. 设置this.checked = false ,长度现在是限制。

 $(document).ready(function(){ $('input[type=checkbox]').on('change', function(evt) { var limit = 3; if ($('form input[type=checkbox]:checked').length >= limit + 1) { console.log('before: ' + $('input[type=checkbox]:checked').length); this.checked = false; console.log('after: ' + $('input[type=checkbox]:checked').length); }; }); }); 
 .checkbox{ padding-left: 20px; } .checkbox>label { display: inline-block; position: relative; padding-left: 5px; color: #686868; padding-top: 2px; min-height: 25px; font-family: 'montserrat'; } .checkbox>span{ padding-right: 17px; padding-top: 3px; font-family: 'montserrat'; } .checkbox>label::before { content: ""; display: inline-block; position: absolute; width: 17px; height: 17px; left: 0; top:4px; margin-left: -20px; border: 1px solid #cccccc; border-radius: 3px; /*background-color: #ccc;*/ -webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out; -o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out; transition: border 0.15s ease-in-out, color 0.15s ease-in-out; /*z-index:25156332;*/ } .checkbox>label::after { display: inline-block; position: absolute; width: 16px; height: 16px; left: 0; top: 4px; margin-left: -20px; padding-left: 3px; padding-top: 1px; font-size: 11px; color: #555555; /*z-index:25156332;*/ } .checkbox input[type="checkbox"] { opacity: 0; } .checkbox input[type="checkbox"]:focus + label::before { outline: thin dotted; outline: 5px auto -webkit-focus-ring-color; outline-offset: -2px; } .checkbox input[type="checkbox"]:checked + label{ color: #1E6C97; } .checkbox input[type="checkbox"]:checked + label::after { font-family: 'FontAwesome'; content: "\\f00c"; } .checkbox input[type="checkbox"]:disabled + label { opacity: 0.65; } .checkbox input[type="checkbox"]:disabled + label::before { background-color: #eeeeee; cursor: not-allowed; } .checkbox.checkbox-circle label::before { border-radius: 50%; } .checkbox.checkbox-inline { margin-top: 0; } .fltr-chk-box-bg{ background-color: #E3E7EA; color: #1E6C97; } .checkbox-primary input[type="checkbox"]:checked + label::before { background-color: #1E6C97; border-color: #428bca; } .checkbox-primary input[type="checkbox"]:checked + label::after { color: #fff; } .checkbox input[type="checkbox"]:checked + label + span{ position: relative; z-index: 75456; } .checkbox input[type="checkbox"]:checked + label + span:before{ content: ''; position: absolute; top: 0; right: 0; border-bottom: 13px solid #CCD2D6; border-top: 11px solid #CCD2D6; border-right: 50px solid #CCD2D6; border-left: 6px solid transparent; z-index: -7523; } 
 <script src="https://use.fontawesome.com/aacdcb9275.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="well btcp-multi-lst bg-white margin-btm-0" id="scrollbar"> <form> <div class="checkbox checkbox-primary"> <input id="category1" type="checkbox" value=""> <label for="category1">Manufacturer</label> </div> <div class="checkbox checkbox-primary"> <input id="category2" type="checkbox" value=""> <label for="category2">Trading Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category3" type="checkbox" value=""> <label for="category3">Distributor / Merchant / Stockist</label> </div> <div class="checkbox checkbox-primary"> <input id="category4" type="checkbox" value=""> <label for="category4">Wholesaler / Dealer</label> </div> <div class="checkbox checkbox-primary"> <input id="category5" type="checkbox" value=""> <label for="category5">Exporter</label> </div> <div class="checkbox checkbox-primary"> <input id="category6" type="checkbox" value=""> <label for="category6">Converter</label> </div> <div class="checkbox checkbox-primary"> <input id="category7" type="checkbox" value=""> <label for="category7">Broker</label> </div> <div class="checkbox checkbox-primary"> <input id="category8" type="checkbox" value=""> <label for="category8">Direct Seller</label> </div> <div class="checkbox checkbox-primary"> <input id="category9" type="checkbox" value=""> <label for="category9">Retailer</label> </div> <div class="checkbox checkbox-primary"> <input id="category10" type="checkbox" value=""> <label for="category10">Indenting Agent / Buying Office / Purchasing Agent</label> </div> <div class="checkbox checkbox-primary"> <input id="category11" type="checkbox" value=""> <label for="category11">Carrying & Forwarding Agent</label> </div> <div class="checkbox checkbox-primary"> <input id="category12" type="checkbox" value=""> <label for="category12">Packers</label> </div> <div class="checkbox checkbox-primary"> <input id="category13" type="checkbox" value=""> <label for="category13">Printers</label> </div> <div class="checkbox checkbox-primary"> <input id="category14" type="checkbox" value=""> <label for="category14">Recycling Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category15" type="checkbox" value=""> <label for="category15">Service Provider</label> </div> <div class="checkbox checkbox-primary"> <input id="category16" type="checkbox" value=""> <label for="category16">Warehousing Company</label> </div> <div class="checkbox checkbox-primary"> <input id="category17" type="checkbox" value=""> <label for="category17">Logistics Provider</label> </div> <div class="checkbox checkbox-primary"> <input id="category18" type="checkbox" value=""> <label for="category18">Mining Company</label> </div> </form> </div> 

Here's how to 这是如何

Limit number of checked checkboxes 限制已选中复选框的数量

 // PS: Use your desired selector to target your checkboxes: var $ckb = $(":checkbox[name^=c]").on("change", function(){ var $checked = $ckb.filter(":checked"); $ckb.not($checked).prop("disabled", $checked.length >= 3); }); 
 label{display:block;} 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label><input type="checkbox" name="c1"> Test 1</label> <label><input type="checkbox" name="c2"> Test 2</label> <label><input type="checkbox" name="c3"> Test 3</label> <label><input type="checkbox" name="c4"> Test 4</label> <label><input type="checkbox" name="c5"> Test 5</label> 

Another way of doing it: 另一种方法是:

Add the class "limited-cb" to your input checkboxes tags. 将类“ limited-cb”添加到您的输入复选框标签中。

 <div class="checkbox checkbox-primary">
            <input class="limited-cb" id="category1" type="checkbox" value=""> <label for="category1">Manufacturer</label>
        </div>

Use this jQuery not to let the 4th checkbox be clicked. 使用此jQuery不要让第四个复选框被单击。

$(".limited-cb").click( function(){
        var checkedCount = 0;
        var acceptCheck = false;
        var $cbs = $(".limited-cb");
        for(var i=0; i< $cbs.length; i++){
            var $cb = $cbs.eq(i);
            if($cb.prop("checked") === true){
                checkedCount++;
                if(checkedCount <= 3){
                    acceptCheck = true;
                }else{
                    acceptCheck = false;
                    break;
                }
            }
        }
        if(acceptCheck === false){
            $(this).prop("checked",false);
        }
    });

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

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