jQuery add/remove css class on selected radio

前端 未结 4 723
夕颜
夕颜 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:17

    JavaScript

    'use strict';
    var o = 'label.input-check, label.input-radio';
    $(o).find('input').change(function () {
        $(o).find('input').filter('input[name="' + (this).name + '"]').each(function () {
            $(this).parent(o).toggleClass('checked', (this).checked);
        });
    }).trigger('change');
    

    Demo: http://jsfiddle.net/3uwp9c2n/

    Just optimize if you need.

提交回复
热议问题