我如何仅对一组单选按钮中选定的单选按钮添加和删除类?
<table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" Yes " id="" name="j_id0:j_id37:j_id41"> label for="j_id0:j_id37:j_id41:0"> Yes </label></td><td><input type="radio" value=" No " id="" name="j_id0:j_id37:j_id41"<label for="j_id0:j_id37:j_id41:1"> No </label></td></tr></tbody></table>
$(document).ready(function(){
$("input[type='radio']").click(function(){
var radioValue = $("input:checked").val();
if(radioValue){
//alert("Your are a - " + radioValue);
$("input:checked").toggleClass("checked");
}
});
JSFiddle .
最佳答案
首先从所有单选按钮中删除 checked
类,然后添加到特定的选定单选按钮。这是您可以执行此操作的方法:
if(radioValue){
$("input[type='radio']").removeClass("checked");
$(this).toggleClass("checked");
}
关于javascript - 将类添加到选定的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368337/