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

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

    Just hide them before showing them:

    $(document).ready(function(){ 
        $("input[name$='group2']").click(function() {
            var test = $(this).val();
            $("div.desc").hide();
            $("#"+test).show();
        }); 
    });
    
    0 讨论(0)
  • 2020-11-30 06:15

    I wrote a simple code to unterstand you to how to make a show and hide radio buttons in jquery its very simple

    <div id="myRadioGroup">
    
        Value Based<input type="radio" name="cars" checked="checked" value="2"  />
    
        Percent Based<input type="radio" name="cars" value="3" />
        <br>
        <div id="Cars2" class="desc" style="display: none;">
            <br>
            <label for="txtPassportNumber">Commission Value</label>
           <input type="text" id="txtPassportNumber" class="form-control" />
        </div>
        <div id="Cars3" class="desc" style="display: none;">
            <br>
            <label for="txtPassportNumber">Commission Percent</label>
           <input type="text" id="txtPassportNumber" class="form-control" />
        </div>
    </div>
    </div>
    

    Jquery code

    $(document).ready(function() {
        $("input[name$='cars']").click(function() {
            var test = $(this).val();
    
            $("div.desc").hide();
            $("#Cars" + test).show();
        });
    });
    

    give me comments

    0 讨论(0)
提交回复
热议问题