jQuery add/remove css class on selected radio

前端 未结 4 722
夕颜
夕颜 2021-01-05 15:33

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

4条回答
  •  走了就别回头了
    2021-01-05 16:18

    Assuming your radio buttons utilize the name property to properly group them, when one changes, you can find the radios with the same name, and remove the class, then add the class to the one that was activated.

    Try it out: http://jsfiddle.net/U2AAQ/

    $(document).ready(function () {
        $(':radio').change(function () {
            $(':radio[name=' + this.name + ']').parent().removeClass('style1');
            $(this).parent().addClass('style1');
        });
    });​
    

提交回复
热议问题