首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript使用复选框选择表格的整行(更改背景颜色),并在单击下一个复选框时取消选择

Javascript使用复选框选择表格的整行(更改背景颜色),并在单击下一个复选框时取消选择
EN

Stack Overflow用户
提问于 2020-04-30 06:56:57
回答 2查看 621关注 0票数 4

我有一个表格,我想选择整行(改变背景颜色)。通过复选框选择行,当选择下一行时,必须取消选择前一行。

这是我的桌子

代码语言:javascript
复制
<table id="table" border="1">
  <tr>
    <th></th>
    <th>ID</th>
    <th>Name</th>
    <th>Surname</th>
  </tr>
  <tr>
    <td class='row'>
      <input type='checkbox' class='check' onclick='markRow(0)'>
    </td>
    <td class='row'>-</td>
    <td class='row'>-</td>
    <td class='row'>-</td>
  </tr>
  <tr>
    <td class='row'>
      <input type='checkbox' class='check' onclick='markRow(1)'>       
    </td>
    <td class='row'>-</td>
    <td class='row'>-</td>
    <td class='row'>-</td>
  </tr>
  <tr>
    <td class='row'>
      <input type='checkbox' class='check' onclick='markRow(2)'>       
    </td>
    <td class='row'>-</td>
    <td class='row'>-</td>
    <td class='row'>-</td>
  </tr>
</table>
代码语言:javascript
复制
#table{
  border-collapse: collapse;
}

我将每个具有行的表格单元格命名为“class=”。通过计算特定行所在的间隔并使用for循环,我应该能够为这些表单元格设置背景色。间隔为:第一行为0-3,第二行为4-7,第三行为8-11。

我试过这个:

代码语言:javascript
复制
var clear1 = 0;
var clear2 = 0;
//these two should clear the previous row

var counter = 0;  
//this will ensure that clearing doesn't happen the first time


//function parameter is given by this checkbox from table  
//<input type='checkbox' class='check'onclick='markRow(0)'> 
function markRow(rowNumber){  
  var row = document.getElementsByClassName('row');
  var checkboxes = document.getElementsByClassName('check');

  var interval = rowNumber*4;

  for(var i=interval;i<=interval+3;i++){
    row[i].style = "background-color: dodgerblue;";
  }
  //for example if function gets parameter rowNumber=2, then it will color the cells in interval 8-11

  counter++;
  if(counter>1){
    for(var i=clear1;i<=clear2;i++){
      row[i].style = "";
    }
    checkboxes[clear1].checked = false;
  }
  clear1 = interval;
  clear2 = interval+3;
  //these two will save the interval from the current row and next time, for loop will delete style 
  //using this interval
}

它适用于第一行,但第二行和第三行有时不会勾选,也不会取消选择。我不知道会有什么问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-30 07:14:19

您始终可以清除所有复选框和背景色,然后应用正确的背景色,并使用行索引和复选框索引选中该复选框

此外,您还需要为行分配类,以便使用getElementsByClassName而不是单元格来获取它们。

避免使用全局变量,因为你不需要它们。

小提琴证明:https://jsfiddle.net/en20wa9j/

代码语言:javascript
复制
function markRow(rowNumber) {
  const row = document.getElementsByClassName('rowclass');
  const checkboxes = document.getElementsByClassName('check');
  // clear everything
  for (let i = 0; i < row.length; i++) {
    row[i].style = "background-color: transparent";
    checkboxes[i].checked = false;
  }
  // check the checkbox and color the row
  checkboxes[rowNumber].checked = true;
  row[rowNumber].style = "background-color: dodgerblue;";
}
代码语言:javascript
复制
<table id="table" border="1">
  <tr>
    <th></th>
    <th>ID</th>
    <th>Name</th>
    <th>Surname</th>
  </tr>
  <tr class='rowclass'>
    <td>
      <input type='checkbox' class='check' onclick='markRow(0)'>
    </td>
    <td class='row'>-</td>
    <td class='row'>-</td>
    <td class='row'>-</td>
  </tr>
  <tr class='rowclass'>
    <td>
      <input type='checkbox' class='check' onclick='markRow(1)'>
    </td>
    <td class='row'>-</td>
    <td class='row'>-</td>
    <td class='row'>-</td>
  </tr>
  <tr class='rowclass'>
    <td>
      <input type='checkbox' class='check' onclick='markRow(2)'>
    </td>
    <td class='row'>-</td>
    <td class='row'>-</td>
    <td class='row'>-</td>
  </tr>
</table>

票数 1
EN

Stack Overflow用户

发布于 2020-04-30 07:41:01

问题出在checkboxes[clear1].checked = false;中,因为只有3个复选框,而不像行,所以您应该使用以下代码行:

代码语言:javascript
复制
checkboxes[clear1 / 4].checked = false;

其中,当之前选择行时,clear1 / 4将分别为0、1、2。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61513096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档