How to use jQuery to show/hide divs based on radio button selection?

后端 未结 8 688
暗喜
暗喜 2020-11-30 05:38

I have some radio buttons and I\'d like to have different hidden divs show up based on which radio button is selected. Here\'s what the HTML looks like:

<         


        
8条回答
  •  抹茶落季
    2020-11-30 06:10

    Update 2015/06

    As jQuery has evolved since the question was posted, the recommended approach now is using $.on

    $(document).ready(function() {
        $("input[name=group2]").on( "change", function() {
    
             var test = $(this).val();
             $(".desc").hide();
             $("#"+test).show();
        } );
    });
    

    or outside $.ready()

    $(document).on( "change", "input[name=group2]", function() { ... } );
    

    Original answer

    You should use .change() event handler:

    $(document).ready(function(){ 
        $("input[name=group2]").change(function() {
            var test = $(this).val();
            $(".desc").hide();
            $("#"+test).show();
        }); 
    });
    

    should work

提交回复
热议问题