jQuery在选定的收音机上添加/删除CSS类 [英] jQuery add/remove css class on selected radio
问题描述
我在这里已经阅读了一些解决方案,但是在无法解决这些问题的地方,我的问题有很大不同.基本上,如果选中了单选按钮,则将css类添加到父div.如果未选中单选,请删除css类.听起来很简单?
I've read a few solutions on here, but my problem differs enough where those will not work. Basically if the radio button is checked, add a css class to the parent div. If the radio is not checked, remove the css class. Sounds simple?
我有多个单选按钮组,换句话说,一次将选择多个单选按钮.每个单选按钮组也位于页面的不同部分,由其他html分隔.
I have more than one radio button group, so in other words, there will be multiple radio buttons selected at a time. Each radio button group also is on different parts of the page seperated by other html.
这是我想出的:
$(document).ready(function () {
$('input').click(function () {
if ($('input:not(:checked)')) {
$('div').removeClass('style1');
}
if ($('input').is(':checked')) {
$(this).parent().addClass('style1');
}
});
});
我认为输入正确的位置不是:((checked)),但在下一行它将从所有div元素中删除style1,而不是检查是否已检查其他divs单选按钮子级.
I think I'm on the right path with the input:not(:checked), but on the next line it removes style1 from all div elements instead of checking if other divs radio button child is checked.
我想我要问的是如何编写一个脚本,当单击单选按钮时,将CSS类添加到父级,然后在页面上找到所有带有输入子级的div标签.如果未选中divs子项输入,请除去css类.因此,如果div有一个子输入元素并且选中了该元素,请不要从输入div中删除css类.
I guess what I'm asking is how do I write a script that when a radio button is clicked, add a css class to the parent and then find all div tags on the page that have a child of input. If the divs child input is not checked, remove the css class. So if a div has a child input element and it is checked, don't remove the css class from the inputs div.
我希望这是有道理的.
推荐答案
为什么不使用以下内容: http://jsfiddle .net/Q2bzT/
Why not using the following: http://jsfiddle.net/Q2bzT/
$(document).ready(function () {
$('input').click(function () {
$('input:not(:checked)').parent().removeClass("style1");
$('input:checked').parent().addClass("style1");
});
});
这篇关于jQuery在选定的收音机上添加/删除CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!