我通过jQuery使用了背景颜色来检查复选框。我的背景颜色仅在我点击任何复选框时添加,但默认情况下尚未点击复选框&我不知道为什么我的班级默认情况下没有点击复选框。
$(document).ready(function() {
/*checkbox-background*/
$(".checkbox-primary").on("click", "input[type='checkbox']", function() {
if ($(this).is(":checked")) {
$(this).parent().addClass("fltr-chk-box-bg");
} else {
$(this).parent().removeClass("fltr-chk-box-bg");
}
});
/*checkbox-background-ends*/
});

.checkbox {
padding-left: 30px;
}
.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 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;
}
.fltr-panel-group {
margin-bottom: 0px;
}
.fltr-panel {
border-bottom: none;
}
.fltr-panel:last-child {
border-bottom: 1px solid #ddd;
}
.fltr-panel-title>a {
padding: 18px 8px 19px 8px;
font-size: 1.21em;
color: #2B3439;
font-family: 'montserrat';
}
.panel-group .panel+.panel.fltr-panel {
margin-top: 0px;
}
.panel-group.fltr-panel-group .panel-heading+.panel-collapse>.panel-body.fltr-panel-body {
border-top: none;
padding: 0px 5px 5px 5px;
}
.fltr-and-reset {
padding-top: 14px;
padding-bottom: 5px;
}
.fltr-and-reset a {
vertical-align: middle;
font-size: 0.78em;
}
.fltr-and-reset>.pull-right {
font-size: 1.07em;
color: #1E6C97;
font-family: 'montserrat';
}
.fltr-and-reset>.text-left {
font-size: 1.14em;
color: #302F2F;
line-height: 1.647;
font-family: 'montserrat';
}

<!-- 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>
<form>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="" checked>
<label>North America</label><span class="pull-right">75</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="" checked>
<label>South America</label><span class="pull-right">98</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">23</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South Asia</label><span class="pull-right">47</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">53</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">55</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">26</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">23</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">78</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">90</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">67</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">88</span>
</div>
</form>
&#13;
答案 0 :(得分:1)
在document.ready()
中添加以下代码,这将循环遍历所有checked
输入并添加该类。
$("input:checked").each(function(){
$(this).parent().addClass("fltr-chk-box-bg");
});
如下所示:
$(document).ready(function() {
/*checkbox-background*/
$(".checkbox-primary").on("click", "input[type='checkbox']", function() {
if ($(this).is(":checked")) {
$(this).parent().addClass("fltr-chk-box-bg");
} else {
$(this).parent().removeClass("fltr-chk-box-bg");
}
});
/*checkbox-background-ends*/
$("input:checked").each(function(){
$(this).parent().addClass("fltr-chk-box-bg");
});
});
&#13;
.checkbox {
padding-left: 30px;
}
.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 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;
}
.fltr-panel-group {
margin-bottom: 0px;
}
.fltr-panel {
border-bottom: none;
}
.fltr-panel:last-child {
border-bottom: 1px solid #ddd;
}
.fltr-panel-title>a {
padding: 18px 8px 19px 8px;
font-size: 1.21em;
color: #2B3439;
font-family: 'montserrat';
}
.panel-group .panel+.panel.fltr-panel {
margin-top: 0px;
}
.panel-group.fltr-panel-group .panel-heading+.panel-collapse>.panel-body.fltr-panel-body {
border-top: none;
padding: 0px 5px 5px 5px;
}
.fltr-and-reset {
padding-top: 14px;
padding-bottom: 5px;
}
.fltr-and-reset a {
vertical-align: middle;
font-size: 0.78em;
}
.fltr-and-reset>.pull-right {
font-size: 1.07em;
color: #1E6C97;
font-family: 'montserrat';
}
.fltr-and-reset>.text-left {
font-size: 1.14em;
color: #302F2F;
line-height: 1.647;
font-family: 'montserrat';
}
&#13;
<!-- 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>
<form>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="" checked>
<label>North America</label><span class="pull-right">75</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="" checked>
<label>South America</label><span class="pull-right">98</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">23</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South Asia</label><span class="pull-right">47</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">53</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">55</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">26</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">23</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">78</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">90</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">67</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">88</span>
</div>
</form>
&#13;
答案 1 :(得分:0)
在准备好初始化后把它放到:
$("input[type='checkbox']:checked").each(function(){
this.parentNode.classList.add('fltr-chk-box-bg');
});
答案 2 :(得分:0)
此代码将onload还执行单击复选框时运行的所有代码。
我不是只触发背景颜色,而是运行该功能,因为在点击时通常会有其他副作用(显示/隐藏div等)。
还要注意使用toggleClass
function whenClicked() {
$(this).closest("div").toggleClass("fltr-chk-box-bg",this.checked);
// anything else that should happen on click
}
$(function() {
$(".checkbox-primary").on("click", "input[type='checkbox']", whenClicked);
// not quite sure why I could not just trigger click on the above
// but have to run an each
$(".checkbox-primary input[type='checkbox']:checked").each(whenClicked);
});
&#13;
.checkbox {
padding-left: 30px;
}
.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 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;
}
.fltr-panel-group {
margin-bottom: 0px;
}
.fltr-panel {
border-bottom: none;
}
.fltr-panel:last-child {
border-bottom: 1px solid #ddd;
}
.fltr-panel-title>a {
padding: 18px 8px 19px 8px;
font-size: 1.21em;
color: #2B3439;
font-family: 'montserrat';
}
.panel-group .panel+.panel.fltr-panel {
margin-top: 0px;
}
.panel-group.fltr-panel-group .panel-heading+.panel-collapse>.panel-body.fltr-panel-body {
border-top: none;
padding: 0px 5px 5px 5px;
}
.fltr-and-reset {
padding-top: 14px;
padding-bottom: 5px;
}
.fltr-and-reset a {
vertical-align: middle;
font-size: 0.78em;
}
.fltr-and-reset>.pull-right {
font-size: 1.07em;
color: #1E6C97;
font-family: 'montserrat';
}
.fltr-and-reset>.text-left {
font-size: 1.14em;
color: #302F2F;
line-height: 1.647;
font-family: 'montserrat';
}
&#13;
<!-- 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>
<form>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="" checked>
<label>North America</label><span class="pull-right">75</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="" checked>
<label>South America</label><span class="pull-right">98</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">23</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South Asia</label><span class="pull-right">47</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">53</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">55</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">26</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">23</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">78</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">90</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>North America</label><span class="pull-right">67</span>
</div>
<div class="checkbox checkbox-primary">
<input type="checkbox" value="">
<label>South America</label><span class="pull-right">88</span>
</div>
</form>
&#13;