Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅使用javascript删除多个选定表行_Javascript - Fatal编程技术网

仅使用javascript删除多个选定表行

仅使用javascript删除多个选定表行,javascript,Javascript,我想使用javascript删除表中选定的行,但问题是一旦删除了一行,索引就会改变 这个问题令人困惑,因为只有特定的行包含复选框,并且可以添加更多的复选框 我已经可以使用javascript在数组内部获得我想要删除的所有正确的行索引了,但是接下来我该如何删除行而不点击错误的行,也不使用prototype或jquery。纯javascript 非常感谢您的帮助 <table class="example_table" id="mytable"> <tbody>

我想使用javascript删除表中选定的行,但问题是一旦删除了一行,索引就会改变

这个问题令人困惑,因为只有特定的行包含复选框,并且可以添加更多的复选框

我已经可以使用javascript在数组内部获得我想要删除的所有正确的行索引了,但是接下来我该如何删除行而不点击错误的行,也不使用prototype或jquery。纯javascript

非常感谢您的帮助

 <table class="example_table" id="mytable">
    <tbody>
          <tr>
                <td><input type="text" id="top row" /></td>
          </tr>

          <tr>
                <td><input type="button" onclick="addNewCheckboxRow()" /></td> 
          </tr>

          <tr>
                <td><input type="button" /></td> 
          </tr>

          <tr>
                <td><input type="checkbox" value="1" id="check1" /></td>  //row that may be deleted
          </tr>

          <tr>
                <td><input type="checkbox" value="2" id="check2" /></td>  //row that may be deleted
          </tr>

          <tr>
                <td><input type="checkbox" value="3" id="check3" /></td>  //row that may be deleted
          </tr>
          <tr>
                <td><input type="checkbox" value="4" id="check4" /></td>  //row that may be deleted
          </tr>   

          <tr>
                <td><input type="button" /></td> 7
          </tr>
        </tbody>
    </table>
您的数组从较大的元素到较低的元素

function compareNumbers(a, b)
{
  return b - a;
}

然后从最大值到最小值进行删除。

或者通过向后循环表来获取索引。。。(还要注意,
.sort()
比较函数需要返回负、正或零,而不是布尔值,所以使用
返回b-a;
)哈哈!我没有想到排序,我甚至不知道你可以倒循环。谢谢
function compareNumbers(a, b)
{
  return b - a;
}