jQuery radio onchange toggle class of parent element?

前端 未结 7 956
无人及你
无人及你 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:31

    Here is the solution which worked great for me:

    var $boxes=$('input:radio');
    var $parents = $boxes.parent();
    $boxes.click(function(){
        $parents.filter('.selected').removeClass('selected');
        $(this).parent().addClass('selected');
    });
    $boxes.filter(':checked').parent().addClass('selected');
    

    Features:

    • Faster. Only selects list of checkboxes once.
    • Does not rely on :checked. I'm not sure if there is a certain order in which "clicked" and "changed" will be executed across browsers.
    • uses :radio instead of [type="radio"] which is jQuery recommended
    • sets class on the parent for the default value of radio button.

    Hope this helps!

    0 讨论(0)
  • 2021-01-11 19:37

    Try this:

    if($(this).is(':checked')) {
        $(this).parent().siblings('td.green').removeClass('green');
        $(this).parent().addClass('green');
    }
    
    0 讨论(0)
  • 2021-01-11 19:38

    my proposal is :

    $('#myRadio').change(function() {           
        _parent = $(this).parent();
        if($(this).is(':checked'))  {
           _parent.addClass('green');
        } else {                              
           _parent.removeClass('green');
        }
    });
    
    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2021-01-11 19:46

    i think this is the best and more flexible way to do this:

    forget the tables (google know many reason for that) and use wrappers like below

    <div id="radio_wrapper">
        <span class="radio">
            <label for="myRadio1" class="myRadio">Some text 1 </label>
            <input type="radio" value="txt1" name="myRadio" id="myRadio1" class="myRadio" />
        </span>
        <span class="radio">
            <label for="myRadio2" class="myRadio">Some text 2 </label>
            <input type="radio" value="txt2" name="myRadio" id="myRadio2" class="myRadio" />
        </span>
        <span class="radio">
            <label for="myRadio3" class="myRadio">Some text 3 </label>
            <input type="radio" value="txt3" name="myRadio" id="myRadio3" class="myRadio" />
        </span>
    </div
    

    format with CSS like this

    span.radio {
        background-color: #cccccc;
    }
    span.currentGreen {
        background-color: #ccffcc;
    }
    

    and using this script you can do exactly the same as you want

    $('.myRadio').change(function() {           
        $('.currentGreen').removeClass('currentGreen'); // remove from all
        if ($(this).is(':checked')) {
            $(this).parent().addClass('currentGreen'); // add to current
        }
    });
    

    i don't like to use filter() and find() because they use unnecessary resources in this case.

    0 讨论(0)
  • 2021-01-11 19:50

    You shouldn't use the change() event on radio buttons and checkboxes. It behaves a little dodgy and inconsistent across browsers (it causes problems in all versions of IE)

    Use the click() event instead (don't worry about accessibility, because the click event will also be fired if you activate/select a radio button with the keyboard)

    And as the others here pointed out, resetting the green is easy as well:

    So simply change your code to

    $('#myRadio').click(function() {           
        $(this).parents("tr").find(".green").removeClass("green");
    
        if($(this).is(':checked'))  {
            $(this).parent().addClass('green');
        }
    });
    

    EDIT: as requested in comment, also change the previous td:

    $('#myRadio').click(function() {           
        $(this).parents("tr").find(".green").removeClass("green");
    
        if($(this).is(':checked'))  {
            $(this).parent().prev().andSelf().addClass('green');
        }
    });
    

    or even better, turning all td elements of the parent row green:

    $('#myRadio').click(function() {           
        $(this).parents("tr").find(".green").removeClass("green");
    
        if($(this).is(':checked'))  {
            $(this).parents("tr").find("td").addClass('green');
        }
    });
    
    0 讨论(0)
提交回复
热议问题