根据已检查的电台添加课程并删除课程? [英] Add class and remove class based on checked radio?
问题描述
这是我的jsbin, http://jsbin.com/ugufi/11 我删除了任何尝试我删除了该类,因为它只是使警报保持未定义状态.
Here is my jsbin, http://jsbin.com/ugufi/11 I removed any attempt I made to remove the class, because it just kept alerting undefined.
我的问题是,当我将选中的类添加到目标中时(如上所述),我无法删除目标的类.
My problem is when I add the checked class to my target I (as said above) fail to remove the class of the target.
我不能使用父选择器或兄弟选择器,因为目标与我的嵌套无线电输入是分开的.
I cannot use parent or sibling selectors, because the target is separate from my nested radio inputs.
选中新电台后,它会继续添加到ex类中
When a new radio is checked It keeps adding onto the class ex
<div class="radio1 radio2 radio3 radio4"></div>
我需要什么(我知道这很简单,但我不确定要使用什么).
What I need (which I know is very simple but Im not sure what to use).
<div class="radio1"></div><!-- Radio input value with radio1 is checked -->
然后当我检查新的收音机输入
then when I check a new radio input
<div class="radio4"></div><!-- Radio input value with radio4 is checked -->
推荐答案
那是不对的,您可以使用siblings
方法,您的元素不必是所选元素的下一个同级对象.通过使用addClass
,您应该删除以前添加的类,可以使用removeClass
方法删除这些类,也可以通过设置新值来覆盖类的属性/属性:
That's not true, you can use siblings
method, your element doesn't have to be a very next sibling of the selected element. By using addClass
you should remove previously added classes, you can either remove those classes using removeClass
method or overwrite the class attribute/property by setting a new value:
$('.moon-generator-attr').change(function() {
var $this = $(this);
$this.siblings('.icon-wrap').find('a').attr('class', function(){
return 'icon icon-' + $this.val();
});
});
这篇关于根据已检查的电台添加课程并删除课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!