Jquery 单击“将类添加到标签”

Jquery 单击“将类添加到标签”,jquery,Jquery,我正在写一个测验脚本。我隐藏了单选按钮,并希望使标签类(单选按钮伪)可点击 我有: <li class="pos-3"> <input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/> <label class="" for="radio1_<?php echo $result

我正在写一个测验脚本。我隐藏了单选按钮,并希望使标签类(单选按钮伪)可点击

我有:

<li class="pos-3">
   <input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
   <label class="" for="radio1_<?php echo $result['id'];?>"><?php echo $result['answer4'];?></label>
</li>
但是失败得很惨

如果有任何建议,电台正在注册。但是这个类没有被添加


每个问题有4个选项。

您需要为单选元素注册更改处理程序,然后可以使用.toggleClass()切换标签的选中类

jQuery(function () {
    $('input[type="radio"]').change(function () {
        $('input[name="' + this.name + '"]').not(this).next().removeClass('checked');
        $(this).next().toggleClass('checked', this.checked)
    })
})

演示:

您只需将类
选中
添加到单击的单选按钮,然后从其他单选按钮中删除该类,如下所示:

$('input[type="radio"]').change(function () { 
    $('input[type="radio"]').next().removeClass('checked');
    $(this).next().addClass('checked');    
});

$('input[type="radio"]').change(function () { 
    $('input[type="radio"]').next().removeClass('checked');
    $(this).next().addClass('checked');    
});