2013-05-17 97 views
1

我想用复选框显示和隐藏表格。表出现并消失没有问题。但复选框没有被检查。我有Jquery v1.8.2。我有以下代码:HTML复选框不起作用

<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> 

回答

5

尝试这种方式 -

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

工作演示-->http://jsfiddle.net/pmNAe/

+0

thanks..its工作 – user1763032

+0

欢迎您! –

0

试试这个JSFIDDLE

注意:您可以使用变化的,而不是点击但却改变只是Firefox模糊后会被解雇。

$(function(){ 

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

你可以尝试像

$('#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";  
); 
2

尝试

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

演示:Fiddle

0

入住此提琴的解决方案:

JSFiddle

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

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