修改被选中的单选项的样式,但是添加了样式之后修改选项样式仍然存在,即同时出现两个被选中的样式,以下是我的代码,请问各位应该如何修改?
<p class="choice">
<label>
<input type="radio" class="a" name="1"><span class="aFont">aaaaa</span>
</label>
<label>
<input type="radio" class="b" name="1"><span class="bFont">bbbbb</span>
</label>
<label>
<input type="radio" class="c" name="1"><span class="cFont">ccccc</span>
</label>
<label>
<input type="radio" class="d" name="1"><span class="dFont">ddddd</span>
</label>
</p>
<script>
$(document).ready(function(){
$(".choice input[type = 'radio']").on("click",function(){
if ($(".choice input:checked")){
$(this).siblings().css("background","red");
}
});
});
</script>
仅有的幸福2017-05-19 10:39:10
<p class="choice">
<label>
<input type="radio" class="a" name="1"><span class="aFont">aaaaa</span>
</label>
<label>
<input type="radio" class="b" name="1"><span class="bFont">bbbbb</span>
</label>
<label>
<input type="radio" class="c" name="1"><span class="cFont">ccccc</span>
</label>
<label>
<input type="radio" class="d" name="1"><span class="dFont">ddddd</span>
</label>
</p>
<script>
$(document).ready(function(){
$(".choice input[type = 'radio']").on("click",function(){
if ($(".choice input:checked")){
$(this).parent().css("background" , "green").siblings().css("background","red");
}
});
});
</script>
你选中的 是input 不是label 注意 $(this).parent().css("background","green").siblings()
某草草2017-05-19 10:39:10
代码没测试, 思路就是先移除所有背景色,再给点击的那一个添加背景色, 你这个if判断是不需要的
<script>
$(document).ready(function(){
$(".choice input[type = 'radio']").on("click",function(){
$(".choice input[type = 'radio']").css("background","");
$(this).css("background","red");
});
});
</script>