jQuery radio onchange toggle class of parent element?

前端 未结 7 1101
无人及你
无人及你 2021-01-11 19:28

Please have a look on the following:

$(\'#myRadio\').change(function() {           

    if($(this).is(\':checked\'))  {
        $(this).parent().addClass(\'         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-11 19:39

    Try something like this:

    if($(this).is(':checked'))  {
        $(this).parent().parent().parent().find('.green').removeClass('green');
        $(this).parent().addClass('green');
    }
    

    This will find the table element of your current radio button grouping, find any elements with the green class, and remove the class.

    Alternatively, if you only have one radio button group on the page, it would be simpler to just do:

    $('.green').removeClass('green');
    

提交回复
热议问题