gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:23:48 27 4
gpt4 key购买 nike

由于我设计了自定义复选框,因此我想限制该复选框。我可以轻松地为默认复选框添加限制,但就我而言,由于我自己的设计,它不起作用,而且我不知道自定义复选框选择限制。请帮助我摆脱困境。

$(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') 函数返回单个 bool 值,您不能使用该 length 属性。

建议的方法是使用 $('input[type="checkbox"]:checked').length > 1) 选择器选择所有选中的复选框。如果它们的数量大于例如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>

关于javascript - 自定义复选框选择限制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820907/

27 4 0
文章推荐: javascript - templateUrl 中的相对 url 与 Angular 4 和 sails 不工作
文章推荐: flutter - 升级升级后出现错误 'textBaseline != null'
文章推荐: flutter - 如何将Future >转换为List
文章推荐: JavaScript 函数