jQuery radio button show div

前端 未结 4 704
余生分开走
余生分开走 2021-01-15 18:53

Hey guys, what function could I use to say, if this radio button is checked show this \'div\'. Thanks in advance.

相关标签:
4条回答
  • 2021-01-15 19:33

    HTML

    <form id='form-id'>
        <input id='watch-me' name='test' type='radio' /> Show Div<br />
        <input name='test' type='radio' /><br />
        <input name='test' type='radio' />
    </form>
    <div id='show-me' style='display:none'>Hello</div>
    

    jQuery

    $('#form-id').change(function() {
        if ($('#watch-me').attr('checked')) {
            $('#show-me').show();
        } else {
            $('#show-me').hide();
        }
    });
    

    You can see it in action here: http://jsfiddle.net/wmKGd/

    UPDATE 25.01.2013
    After Upgrading from jQuery Library 1.8.x to 1.9.x
    Please use instead of
    jQuery Library 1.8.x

    jQuery('#some-id').attr('checked') 
    

    jQuery Library 1.9.x

    jQuery('#some-id').prop('checked')
    

    You can see it with updated script in action here: http://jsfiddle.net/9XXRY/

    0 讨论(0)
  • 2021-01-15 19:34
    $("myradiobuttonselector").change(function () {
      if ($(this).attr("checked")) {
        $("mydivSelector").show();
      }
      else {
       $("mydivSelector").hide();
      }
    });
    
    0 讨论(0)
  • 2021-01-15 19:48

    I liked @tinifni's jsfiddle. Here's one with 3 divs that show or hide depending on the radio.

    http://jsfiddle.net/dbwest/wmKGd/572/

    $('#form-id').change(function() {
        if ($('#watch-me').attr('checked')) {
            $('#show-me').show();
        } else {
            $('#show-me').hide();
        }
        if ($('#watch-me2').attr('checked')) {
            $('#show-me2').show();
        } else {
            $('#show-me2').hide();
        }
        if ($('#watch-me3').attr('checked')) {
            $('#show-me3').show();
        } else {
            $('#show-me3').hide();
        }
    });
    
    0 讨论(0)
  • 2021-01-15 19:49

    This is what I use. Replace “mydiv” with the id of your radio button and “content1” with the content you want to show/hide.

    Post some html if you need further help.

    $(document).ready(function(){
       $('#content1').hide();
    
       $('a').click(function(){
          $('#content1').show('slow');
       });
    
       $(‘#mydiv’).click(function(){
          $('#content1').hide('slow');
       })
    });
    

    Rick

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