我希望在切换开启时将#bounds的边框颜色更改为绿色,但现在当有多个切换开关切换时,它会在点击其他切换时更改第一个切换颜色。
我只是希望能够进行多次切换,并为green = on和gray = off维护第一个切换behivor。
$("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>
答案 0 :(得分:1)
您的JavaScript当前尝试使用ID switchtoggle添加和删除绿色/灰色类。当第一次出现的是第一个切换元素时,它就切换了。
要切换单击的无线电输入的.switchtoggle元素,请搜索单击复选框的下一个父级(使用jQuery函数parents()),该复选框具有类.switchtoggle并删除/添加该类:
$("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>