When Checkbox is Checked Expand Div

后端 未结 7 1609
野性不改
野性不改 2021-02-10 16:52

I have a checkbox:


This content should app
相关标签:
7条回答
  • 2021-02-10 17:12

    This will show it when the checkbox is checked, and hide it again when the checkbox is unchecked:

    $('#mycheckbox').change(function() {
        $(this).next('div').toggle();
    });
    

    ...although it would be better if you'd assign that DIV an id, so it could be selected more quickly:

    <input type="checkbox" name="mycheckbox" id="mycheckbox" value="0" />
    <div id="mycheckboxdiv" style="display:none">
        This content should appear when the checkbox is checked
    </div>
    
    <script type="text/javascript">
    $('#mycheckbox').change(function() {
        $('#mycheckboxdiv').toggle();
    });
    </script>
    

    http://jsfiddle.net/mblase75/pTA3Y/

    If you want to show the div without hiding it again, replace .toggle() with .show().

    0 讨论(0)
  • 2021-02-10 17:13

    Attach a change event, and check whether a checkbox is checked or not. If the checkbox is checked, show the div. Otherwise, hide it:

    $('#mycheckbox').change(function(){
        if(this.checked) {
            $(this).next().show();
        } else {
            $(this).next().hide();
        }
    });
    

    You should also have a look at the jQuery docs, before asking such a trivial question.

    0 讨论(0)
  • 2021-02-10 17:17
    $("#mycheckbox").change(function(){ 
        $(this).next().toggle(this.checked); 
    });
    
    0 讨论(0)
  • 2021-02-10 17:17
    $("input#mycheckbox").click(function() {
        if($(this).is(":checked") {
            $("div").slideDown();
        }
        else {
            $("div").slideUp();
        }
    })
    
    0 讨论(0)
  • 2021-02-10 17:22
    if ($("#mycheckbox").checked) {
       $("div").style.display = "block";
    }
    
    0 讨论(0)
  • 2021-02-10 17:24

    If you are looking for only css solution, then this can help you.

    #toggle-content{
    display:none;
    }
    #mycheckbox:checked ~ #toggle-content{
      display:block;
      height:100px;
    
    }
    

    Fiddle

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