0

代码和演示:http ://spotsync.com/res.net/portals/reo/snippets/radio.html

我正在尝试将绿色背景添加到选中状态的单选按钮和/或复选框。

复选框正常工作,但是当检查了不同的广播按钮时,其背景颜色不会删除。我在这里想念什么?

4

3 回答 3

1

请记住添加name=到复选框,这将很好地工作

$('input').change(function() {
   $('input[name="' + this.name + '"]').parent().removeClass('greenBG')
   .find(':checked').parent().addClass('greenBG');
});
于 2011-03-28T16:48:43.657 回答
1

当您使用收音机时,您使用的是一组输入,因此您不能只使用this is checked条件。您需要过滤该组。

在这里查看:http: //jsfiddle.net/PCkXS/

于 2011-03-28T16:30:17.110 回答
0

我知道如何解决它,我会在一分钟内给你小提琴:-)

这完美地工作:http: //jsfiddle.net/maniator/CujPG/2/

$(document).ready(function() {
    $('input').change(function() {
        if ($(this).is(':checked')) {
            var children = $('.greenBG').children('input:not(:checked)'); 

            if(children.length > 0){
                $(children).each(function(){
                    $(this).parent().removeClass('greenBG');
                })
            }
            $(this).parent().addClass('greenBG');
        } 
    });
});
于 2011-03-28T16:26:11.937 回答