隐藏和/或显示表列

时间:2012-05-05 04:14:26

标签: javascript jquery html css

我需要一个只能显示某些列的表,具体取决于是否选中了复选框。如果选中,我希望显示所有列。如果没有选中,我需要它只显示“每周”类列。 我希望尽可能少地使用JavaScript(如果有的话),并且需要跨浏览器兼容

 <table>
   <tr class="weekly">
     <td></td>
   </tr>
   <tr class="overall">
     <td></td>
   </tr>
 </table>
 <input type="checkbox" name="data" value="Show Overall Data"  />

简单地说,我需要一个隐藏和显示某些列的表,具体取决于复选框的状态。

另外,我不擅长编码所以我需要有人粘贴整个代码,包括HTML标签,什么不是。

4 个答案:

答案 0 :(得分:2)

的Javascript

function check()
{

  if(document.getElementById("weekly").style.display == "none")
    document.getElementById("weekly").style.display = "block";
  else
    document.getElementById("weekly").style.display = "none";
  if(document.getElementById("overall").style.display == "block")
   document.getElementById("overall").style.display = "none";
  else
   document.getElementById("overall").style.display = "block";

}

和HTML

<table>
   <tr class="weekly" id="weekly" style="display:none">
     <td>ABBB</td>
   </tr>
   <tr class="overall" id="overall" style="display:none">
     <td>BCC</td>
   </tr>
 </table>
<input type="checkbox" name="data" value="Show Overall Data" onclick="check()"/>

答案 1 :(得分:1)

编辑:完整的HTML在这里。使用jquery

<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script>

$(document).ready(function(){


if ($("input:checkbox").is(":not(:checked)")){
    $('tr').hide();
   $('tr.weekly').show();
}

});
</script>
</head>
<body>
<input type="checkbox" name="data"/>

<table>
   <tr class="weekly">
     <td>WEEEKLY</td>
   </tr>
   <tr class="overall">
     <td>OVERLLL</td>
   </tr>
 </table>

</body>
</html>

答案 2 :(得分:0)

使用jquery

$(document).ready(function () {
    $('.overall').hide();
}); 

$("#data").is(':checked')
    ? $(".overall").show() 
    : $(".overall").hide();

将name =“data”更改为Id =“data”

答案 3 :(得分:0)

基于早期stackoverflow答案中指向的@anu示例, 这是jsbin中的一个实例。

http://jsbin.com/ehodul/4/edit#javascript,html,live

以下是(大部分)代码,但同样, 我鼓励你修改jsbin上的例子。

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="jquery.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  table { padding: 0; border-collapse: collapse; }
  table th { background-color: #eee; }
  table td, th { border: 1px solid #d7d7d7; padding: 7px; }
</style>
</head>
<body>


<select id="myOptions">
  <option value="1">hide col 1</option>
  <option value="2">hide col 2</option>
  <option value="3">hide col 3</option>
  <option value="4">hide col 4</option>
</select>  

  <p>&nbsp;</p>

<table id="myTable" cellspacing="0" cellpadding="0">
  <thead>
    <tr>
      <th>col 1</th>
      <th>col 2</th>
      <th>col 3</th>
      <th>col 4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1abcdefg</td>
      <td>2abcdefg</td>
      <td>3abcdefg</td>
      <td>4abcdefg</td>
    </tr>
    <tr>
      <td>1abcdefg</td>
      <td>2abcdefg</td>
      <td>3abcdefg</td>
      <td>4abcdefg</td>
    </tr>
    <tr>
      <td>1abcdefg</td>
      <td>2abcdefg</td>
      <td>3abcdefg</td>
      <td>4abcdefg</td>
    </tr>
    <tr>
      <td>1abcdefg</td>
      <td>2abcdefg</td>
      <td>3abcdefg</td>
      <td>4abcdefg</td>
    </tr>
  </tbody>
  </table>  

  <script type="text/javascript">
function hideColumn(columnIndex) {
   $('#myTable thead th, #mytable tbody td').show();
   $('#mytable thead th:nth-child('+(columnIndex)+'), #mytable tbody td:nth-child('+(columnIndex)+')').hide();

}

    $("#myOptions").change(function() {
      selectedVal = $(this).val();
      console.log(selectedVal);
      hideColumn( selectedVal );
    });

  </script>
</body>
</html>
相关问题