JavaScript 选择元素

发布于 2024-09-29 09:18:10 字数 92 浏览 0 评论 0原文

如何通过了解列索引来选择表列中的所有复选框?

LE:这需要用普通的 Javascript 来解决,而不是 jQuery

How can I select all of the check boxes from a table column by knowing the column index?

LE: This needs to be addressed in plain Javascript, not jQuery

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

影子是时光的心 2024-10-06 09:18:10

最简单的方法是,正如 Ben Lee 建议的那样,设置一个 id 属性,您可以在表元素本身上轻松找到该属性。然后,您可以使用以下命令来查找表:

var myTable = document.getElementById("<idattributevalue>");

现在我们必须迭代具有相关列索引的所有行(我们将其称为“myIndex”),我们现在有一个函数可以用来查找您的所有表checkboxes:

function findMyCheckboxes(myTable, myIndex) {

  var myCheckboxes = [];
  var cell = null;
  var allInputs = null;

  var myRows = myTable.rows;

  for (var i = 0; i < myRows.length; i++) {
    // Get the cell for each row at the index we know
    cell = myRows[i].cells[myIndex];
    // Get all input tags in that cell
    allInputs = cell.getElementsByTagName("input");
    // Only pick the inputs which are checkboxes
    for (var j = 0; j < allInputs.length; j++) {
      if (allInputs[j].type == "checkbox") {
        myCheckboxes.push(allInputs[j]);
      }
    }
  }

  return(myCheckboxes);

}

当然,这段代码可能有一些语法错误。请随意指出它们。

The easiest way to do this is to, as Ben Lee recommended, set an id attribute which you can find easily on the table element itself. You can then use the following to find your table:

var myTable = document.getElementById("<idattributevalue>");

Now we have to iterate through all of the rows with the column index in question (we'll call it 'myIndex'), we now have a function we can use to find all your checkboxes:

function findMyCheckboxes(myTable, myIndex) {

  var myCheckboxes = [];
  var cell = null;
  var allInputs = null;

  var myRows = myTable.rows;

  for (var i = 0; i < myRows.length; i++) {
    // Get the cell for each row at the index we know
    cell = myRows[i].cells[myIndex];
    // Get all input tags in that cell
    allInputs = cell.getElementsByTagName("input");
    // Only pick the inputs which are checkboxes
    for (var j = 0; j < allInputs.length; j++) {
      if (allInputs[j].type == "checkbox") {
        myCheckboxes.push(allInputs[j]);
      }
    }
  }

  return(myCheckboxes);

}

This code, of course, probably has some syntax errors. Feel free to point them out.

被翻牌 2024-10-06 09:18:10

假设没有 colspans 或 rowspans,那么你可以使用 jQuery 相当简单地做到这一点:

$('#mytable tr td:nth-child('+index+') input[type="checkbox"]');

@Dustin Laine 添加了一个好点:第 n 个子索引从 1 开始,而不是 0。

Assuming no colspans or rowspans, then you can do it fairly simply with jQuery:

$('#mytable tr td:nth-child('+index+') input[type="checkbox"]');

@Dustin Laine added a good point: The nth-child index starts at 1, not 0.

别挽留 2024-10-06 09:18:10

如果您打算用纯 JavaScript 执行此操作,我建议您向每个复选框添加一个类名,以将其与表列关联起来。因此,您的表格将如下所示:

<table id="mytable">
        <tr>
                <td><input type="checkbox" class="column-1" name="a"></td>
                <td><input type="checkbox" class="column-2" name="b"></td>
                <td><input type="checkbox" class="column-3" name="c"></td>
        <tr>
        <tr>
                <td><input type="checkbox" class="column-1" name="d"></td>
                <td><input type="checkbox" class="column-2" name="e"></td>
                <td><input type="checkbox" class="column-3" name="f"></td>
        <tr>
        <tr>
                <td><input type="checkbox" class="column-1" name="g"></td>
                <td><input type="checkbox" class="column-2" name="h"></td>
                <td><input type="checkbox" class="column-3" name="i"></td>
        <tr>
</table>

然后您可以选择列中的所有复选框,如下所示:

document.getElementsByClassName('column-'+index)

If you're going to do this in plain javascript, I would recommend adding a class name to each checkbox to associate it with a table column. So your table would look something like this:

<table id="mytable">
        <tr>
                <td><input type="checkbox" class="column-1" name="a"></td>
                <td><input type="checkbox" class="column-2" name="b"></td>
                <td><input type="checkbox" class="column-3" name="c"></td>
        <tr>
        <tr>
                <td><input type="checkbox" class="column-1" name="d"></td>
                <td><input type="checkbox" class="column-2" name="e"></td>
                <td><input type="checkbox" class="column-3" name="f"></td>
        <tr>
        <tr>
                <td><input type="checkbox" class="column-1" name="g"></td>
                <td><input type="checkbox" class="column-2" name="h"></td>
                <td><input type="checkbox" class="column-3" name="i"></td>
        <tr>
</table>

Then you could select all the check boxes in a column like this:

document.getElementsByClassName('column-'+index)
兲鉂ぱ嘚淚 2024-10-06 09:18:10

像这样简单的东西可能会起作用,具体取决于您的表结构。

var colIndex = 1; // 0-based
var table = document.getElementById('mytable');
var numRows = table.getElementsByTagName('tr').length;
for (var i=0; i<numRows; i++)
{
var box = table.getElementsByTagName('tr')[i].getElementsByTagName('td')[colIndex].getElementsByTagName("input")[0];
box.setAttribute("checked", "true"); 
}

Something simple like this might work, depending on your table structure.

var colIndex = 1; // 0-based
var table = document.getElementById('mytable');
var numRows = table.getElementsByTagName('tr').length;
for (var i=0; i<numRows; i++)
{
var box = table.getElementsByTagName('tr')[i].getElementsByTagName('td')[colIndex].getElementsByTagName("input")[0];
box.setAttribute("checked", "true"); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文