javascript - HTML checkbox not working - Stack Overflow

I want to display and hide a table using a checkbox. The table appears and disappears with no problem .

I want to display and hide a table using a checkbox. The table appears and disappears with no problem . But the checkbox is not getting checked . I have Jquery v1.8.2 . i have the following code :

<html>
<head>
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $('#checkbox').toggle(function() {
            document.getElementById('table').style.display = "inline";
        }, function() {
            document.getElementById('table').style.display = "none";
        });
    </script>
</head>
<body>
    <form action="" method="POST" enctype="multipart/form-data">
    <input type="checkbox" id="checkbox">
    <br />
    <table id="table" style="display: none;">
        <tr>
            <td>
                <input type="file" name="file">
                <input type="submit" name="upload" value="upload">
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

I want to display and hide a table using a checkbox. The table appears and disappears with no problem . But the checkbox is not getting checked . I have Jquery v1.8.2 . i have the following code :

<html>
<head>
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $('#checkbox').toggle(function() {
            document.getElementById('table').style.display = "inline";
        }, function() {
            document.getElementById('table').style.display = "none";
        });
    </script>
</head>
<body>
    <form action="" method="POST" enctype="multipart/form-data">
    <input type="checkbox" id="checkbox">
    <br />
    <table id="table" style="display: none;">
        <tr>
            <td>
                <input type="file" name="file">
                <input type="submit" name="upload" value="upload">
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
Share Improve this question edited May 17, 2013 at 11:50 Lakshmana Kumar 1,2493 gold badges17 silver badges34 bronze badges asked May 17, 2013 at 11:32 user1763032user1763032 4271 gold badge9 silver badges24 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 5

Try this way -

$('#checkbox').change(function () {
    if ($(this).is(":checked")) {
        $('#table').show();
    } else {
        $('#table').hide();
    }
});

Working demo --> http://jsfiddle/pmNAe/

Try

$('#checkbox').click(function () {
    if (this.checked) {
        $('#table').show();
    } else {
        $('#table').hide();
    }
});

Demo: Fiddle

You can Try like

$('#checkbox').click( 
    var my_dis = document.getElementById('table').style.display;
     if(my_dis == 'inline')
        document.getElementById('table').style.display = "none";
     else   //if(my_dis == 'none')
        document.getElementById('table').style.display = "inline";       
);

Check the solution in this fiddle:

JSFiddle

$('#checkbox').change(function(){
    var $this = $(this);
    var $table = $("#table");

    if($this.is(":checked"))
        $table.show();
    else
        $table.hide();
});

Try this JSFIDDLE

Note: You can use change instead of click but but change get fired only after blur in firefox.

$(function(){

    $('#checkbox').click(function (){
           if(this.checked){
               $("#table").show(); 
          }else{
               $("#table").hide();  
         }
     });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744201460a4562897.html

相关推荐

  • javascript - HTML checkbox not working - Stack Overflow

    I want to display and hide a table using a checkbox. The table appears and disappears with no problem .

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信