我想在切换打开时将#bounds 的边框颜色更改为绿色,但现在当有多个切换切换时,它会在点击其他切换时更改第一个切换颜色。
我只是希望能够进行多次切换并维护绿色 = 打开和灰色 = 关闭的第一个切换行为。
$("input:radio").click(function(ev) {
if (ev.currentTarget.value == "1") {
$('#switchtoggle').removeClass('gray');
$('#switchtoggle').addClass('green');
} else if (ev.currentTarget.value == "0") {
$('#switchtoggle').addClass('gray');
$('#switchtoggle').removeClass('green');
}
});
#switchtoggle {
padding: 2px;
transition: all .2s ease;
border-radius: 2em;
overflow: auto;
float: left;
color: transparent;
border: 3px solid #e8e8e8;
}
#switchtoggle.active {
border-color: #7FC6A6;
transition: all 1s ease-in-out;
}
#switchtoggle label {
float: left;
width: 3.5em;
height: 1.5em;
margin: 0;
}
#switchtoggle label span {
text-align: center;
display: block;
background: #7FC6A6;
width: 1.75em;
height: 1.5em;
border-radius: 20px;
transition: 350ms all;
float: left;
}
#switchtoggle label input {
position: absolute;
width: 2.0em;
height: 1.25em;
opacity: 0;
}
#switchtoggle label input.off:checked+span {
background-color: #e8e8e8;
color: transparent;
margin-left: 1.75em;
}
#switchtoggle label input.off:checked {
display: none;
}
#switchtoggle label input.on:checked+span {
background: #7FC6A6;
color: transparent;
border-radius: 1em;
float: left;
margin-left: 0;
}
#switchtoggle label input.on:checked {
display: none;
}
.switchtoggle.gray {
border: 3px solid #e8e8e8 !important;
}
.switchtoggle.green {
border: 3px solid #7FC6A6 !important;
}
#switchtoggle label input.off:checked+#switchtoggle {
border: 3px solid #e8e8e8 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
NOFITICATIONS?<br>
<div id="switchtoggle" class="switchtoggle green">
<label>
<input class="on" type="radio" name="notifications" value="1" checked>
<input class="off" type="radio" name="notifications" value="0">
<span></span>
</label>
</div>
<br><br><br> EMAIL?
<br>
<div id="switchtoggle" class="switchtoggle gray">
<label>
<input class="on" type="radio" name="emails" value="1">
<input class="off" type="radio" name="emails" value="0" checked>
<span></span>
</label>
</div>
最佳答案
您的 JavaScript 当前尝试在 ID 为 switchtoggle 的元素中添加和删除绿色/灰色类。由于第一次出现是第一个切换元素,因此它切换了它。
要切换单击的单选输入的 .switchtoggle 元素,搜索具有类 .switchtoggle 的已单击复选框的下一个父级(使用 jQuery 函数 parents())并在那里删除/添加类:
$("input:radio").click(function(ev) {
var label = $(ev.target).parents('.switchtoggle');
if (ev.currentTarget.value == "1") {
label.removeClass('gray');
label.addClass('green');
} else if (ev.currentTarget.value == "0") {
label.addClass('gray');
label.removeClass('green');
}
});
#switchtoggle {
padding: 2px;
transition: all .2s ease;
border-radius: 2em;
overflow: auto;
float: left;
color: transparent;
border: 3px solid #e8e8e8;
}
#switchtoggle.active {
border-color: #7FC6A6;
transition: all 1s ease-in-out;
}
#switchtoggle label {
float: left;
width: 3.5em;
height: 1.5em;
margin: 0;
}
#switchtoggle label span {
text-align: center;
display: block;
background: #7FC6A6;
width: 1.75em;
height: 1.5em;
border-radius: 20px;
transition: 350ms all;
float: left;
}
#switchtoggle label input {
position: absolute;
width: 2.0em;
height: 1.25em;
opacity: 0;
}
#switchtoggle label input.off:checked+span {
background-color: #e8e8e8;
color: transparent;
margin-left: 1.75em;
}
#switchtoggle label input.off:checked {
display: none;
}
#switchtoggle label input.on:checked+span {
background: #7FC6A6;
color: transparent;
border-radius: 1em;
float: left;
margin-left: 0;
}
#switchtoggle label input.on:checked {
display: none;
}
.switchtoggle.gray {
border: 3px solid #e8e8e8 !important;
}
.switchtoggle.green {
border: 3px solid #7FC6A6 !important;
}
#switchtoggle label input.off:checked+#switchtoggle {
border: 3px solid #e8e8e8 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
NOFITICATIONS?<br>
<div id="switchtoggle" class="switchtoggle green">
<label>
<input class="on" type="radio" name="notifications" value="1" checked>
<input class="off" type="radio" name="notifications" value="0">
<span></span>
</label>
</div>
<br><br><br> EMAIL?
<br>
<div id="switchtoggle" class="switchtoggle gray">
<label>
<input class="on" type="radio" name="emails" value="1">
<input class="off" type="radio" name="emails" value="0" checked>
<span></span>
</label>
</div>
关于javascript - 向表单添加多个切换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783098/