首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Javascript -show基于复选框的特定表行

JavaScript - show based on specific table row with checkboxes

In JavaScript, you can use the "onchange" event of checkboxes to show or hide specific table rows based on the checkbox selection. Here is an example of how you can achieve this:

  1. HTML Markup:
代码语言:txt
复制
<table>
  <tr>
    <td><input type="checkbox" onchange="showRow(this, 1)"></td>
    <td>Row 1</td>
  </tr>
  <tr>
    <td><input type="checkbox" onchange="showRow(this, 2)"></td>
    <td>Row 2</td>
  </tr>
  <tr>
    <td><input type="checkbox" onchange="showRow(this, 3)"></td>
    <td>Row 3</td>
  </tr>
</table>
  1. JavaScript Function:
代码语言:txt
复制
function showRow(checkbox, rowNumber) {
  var table = checkbox.parentNode.parentNode.parentNode; // Get the table element
  var row = table.rows[rowNumber]; // Get the specific row based on rowNumber

  if (checkbox.checked) {
    row.style.display = ""; // Show the row
  } else {
    row.style.display = "none"; // Hide the row
  }
}

Explanation:

  • Each checkbox has an "onchange" event that triggers the "showRow" function.
  • The "showRow" function takes two parameters: the checkbox element and the row number.
  • It uses the parentNode property to traverse up the DOM and get the table element.
  • Then, it uses the rows property of the table to get the specific row based on the row number.
  • If the checkbox is checked, it sets the display property of the row to an empty string, which shows the row. Otherwise, it sets the display property to "none", which hides the row.

This approach allows you to selectively show or hide specific table rows based on the checkbox selection. You can modify the HTML markup and JavaScript function according to your specific requirements.

Please note that this answer does not mention any specific cloud computing brands or products as requested.

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券