Неверный флажок checkbox

76
4

Я использовал цвет фона для отмеченного флажка через 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>

спросил(а) 2021-01-27T16:38:39+03:00 4 месяца, 3 недели назад
1
Решение
62

Добавьте следующий код внутри 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>

ответил(а) 2021-01-27T16:38:39+03:00 4 месяца, 3 недели назад
44

Этот код будет загружать и выполнять весь код, который запускается при щелчке по галочке.

Вместо того, чтобы просто запускать фоновый цвет, я запускаю функцию, так как при нажатии на нее у вас есть другие побочные эффекты (показать/скрыть 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);
});
.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>

ответил(а) 2021-01-27T16:38:39+03:00 4 месяца, 3 недели назад
45

поставьте это после готовности к инициализации:

$("input[type='checkbox']:checked").each(function(){
this.parentNode.classList.add('fltr-chk-box-bg');
});

ответил(а) 2021-01-27T16:38:39+03:00 4 месяца, 3 недели назад
Ваш ответ
Введите минимум 50 символов
Чтобы , пожалуйста,
Выберите тему жалобы:

Другая проблема