我正在写一个小测验剧本。我隐藏了单选按钮,并希望使标签类(单选按钮伪)可点击。
我有:
<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>我想我可以用jQuery实现这一点,比如:
$(document).ready(function() {
if($("input.check").is(":checked")){
$(this).label.addClass("checked");
}
});但不幸的是失败。
如果有任何建议,广播点击正在注册。但这门课没有增加。
每个问题有4个(李)选项。
发布于 2014-01-22 03:54:22
您需要为收音机元素注册一个更改处理程序,然后可以使用.toggleClass()来切换标签的选中类
jQuery(function () {
$('input[type="radio"]').change(function () {
$('input[name="' + this.name + '"]').not(this).next().removeClass('checked');
$(this).next().toggleClass('checked', this.checked)
})
})演示:小提琴
发布于 2014-01-22 04:11:32
您只需将类checked添加到单击的单选按钮中,并从其他单选按钮中删除该类,如下所示:
$('input[type="radio"]').change(function () {
$('input[type="radio"]').next().removeClass('checked');
$(this).next().addClass('checked');
});演示
https://stackoverflow.com/questions/21273816
复制相似问题