gpt4 book ai didi

javascript - 选中的复选框背景不起作用

转载 作者:行者123 更新时间:2023-11-28 15:39:15 24 4
gpt4 key购买 nike

我通过 jQuery 为选中的复选框使用了背景色。仅当我单击任何复选框但默认情况下尚未单击复选框时,我的背景颜色才会添加,我不知道为什么默认情况下我的类(class)无法处理已单击的复选框。

$(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>

最佳答案

在您的 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");
});
});
.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>

关于javascript - 选中的复选框背景不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43692945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com