1

我需要一些关于 javascript 的帮助。

这是 JSFIDDLE 中代码的链接:http: //jsfiddle.net/Gopher69/B98kZ/4/

我的 JS 是:

$(document).ready(function() {
//add the Selected class to the checked radio button
$('[type=radio]').parent().addClass("selected");
//If another radio button is clicked, add the select class, and remove it from the       previously selected radio
$('input').click(function() {
    $('input:not(:checked)').parent().removeClass("radio  validate-one-required-by-name product-custom-option").find("class").html("radio  validate-one-required-by-name product-custom-option");
    $('input:checked').parent().addClass("radio  validate-one-required-by-name product-custom-option").find("class").html("radio  validate-one-required-by-name product-custom-option active");
});});

我想调整单选按钮的背景颜色,就像我对 CSS 的悬停效果所做的那样。因此,当单选按钮处于活动状态时,我需要在类中添加“选中”之类的内容。

我找到了这篇文章,但我没有得到前言:jQuery Checkbox selection and change class

如果有人可以帮助我,我将非常感激。

4

3 回答 3

3

你可以使用纯 CSS 来做到这一点:

  input[type="radio"]:checked {
    /* put some style here */
  }
于 2012-12-26T12:11:47.077 回答
0
$("#" + radioelementid).on("change",function(){  
$(this).addClass("testClass")   
//or   
$(this).removeClass("testClass");
});
于 2012-12-26T12:10:12.170 回答
0

基于@Krunal Goswami 的回答——甚至更短:

$("#" + elementId).on("change",function(){
    $(this).toggleClass("checked");
});

笔记

那应该做你的伎俩。虽然我不推荐使用这个。而是尝试使用“自定义复选框”实现/jQuery 插件,因为本机浏览器复选框并不喜欢被样式化(跨浏览器)。

于 2012-12-26T12:24:33.313 回答