代码和演示:http ://spotsync.com/res.net/portals/reo/snippets/radio.html
我正在尝试将绿色背景添加到选中状态的单选按钮和/或复选框。
复选框正常工作,但是当检查了不同的广播按钮时,其背景颜色不会删除。我在这里想念什么?
代码和演示:http ://spotsync.com/res.net/portals/reo/snippets/radio.html
我正在尝试将绿色背景添加到选中状态的单选按钮和/或复选框。
复选框正常工作,但是当检查了不同的广播按钮时,其背景颜色不会删除。我在这里想念什么?
请记住添加name=
到复选框,这将很好地工作
$('input').change(function() {
$('input[name="' + this.name + '"]').parent().removeClass('greenBG')
.find(':checked').parent().addClass('greenBG');
});
当您使用收音机时,您使用的是一组输入,因此您不能只使用this is checked
条件。您需要过滤该组。
在这里查看:http: //jsfiddle.net/PCkXS/
我知道如何解决它,我会在一分钟内给你小提琴:-)
这完美地工作: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');
}
});
});