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

后端 未结 8 710
暗喜
暗喜 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:09

    Below code is perfectly workd for me:

    $(document).ready(function(){
        $('input[type="radio"]').click(function(){
            var inputValue = $(this).attr("value");
            var targetBox = $("." + inputValue);
            $(".box").not(targetBox).hide();
            $(targetBox).show();
        });
    });
    .box{
            color: #fff;
            padding: 20px;
            display: none;
            margin-top: 20px;
        }
        .red{ background: #ff0000; }
        .green{ background: #228B22; }
        .blue{ background: #0000ff; }
        label{ margin-right: 15px; }
    
    
    You have selected red radio button so i am here
    You have selected green radio button so i am here
    You have selected blue radio button so i am here

提交回复
热议问题