Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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 将onclick函数添加到我的表的每一行只记录最后一行_Javascript_Html_Bootstrap 4 - Fatal编程技术网

Javascript 将onclick函数添加到我的表的每一行只记录最后一行

Javascript 将onclick函数添加到我的表的每一行只记录最后一行,javascript,html,bootstrap-4,Javascript,Html,Bootstrap 4,在我的html文件中有一个表,它有3行。我正在尝试将onclick方法动态添加到windows中的每一行。onload将返回我正在通过警报单击的行索引,但无论在何处单击,它都会返回,就像我将单击最后一行一样 这是我的windows.onload,位于标签中我的顶部: window.onload = () => { var table = document.getElementById("myTableTesting"); var rows = t

在我的
html
文件中有一个
,它有3行。我正在尝试将
onclick
方法动态添加到windows中的每一行。onload将返回我正在通过警报单击的行索引,但无论在何处单击,它都会返回,就像我将单击最后一行一样

这是我的windows.onload,位于
标签中我的
顶部:

      window.onload = () => {
        var table = document.getElementById("myTableTesting");
        var rows = table.getElementsByTagName("tr");
        for (let i = 0; i < rows.length; i++) {
          var currentRow = table.rows[i];
          console.log('currentRow', currentRow); // < --- HERE IS SHOWING THE CORRECT ROW.
          currentRow.onclick = () => onClickRowTable(currentRow.rowIndex);
        }
      }

      function onClickRowTable(index) {
        alert(index);
        console.log('onClickRowTable', index);
      }
window.onload=()=>{
var table=document.getElementById(“myTableTesting”);
var rows=table.getElementsByTagName(“tr”);
for(设i=0;ionClickRowTable(currentRow.rowIndex);
}
}
函数onClickRowTable(索引){
警报(索引);
log('onClickRowTable',索引);
}
这是我的桌子:

        <table id="myTableTesting" class="table table-bordered table-hover">
          <thead class="thead-dark" style="text-align: center">
            <tr>
                <th>
                  Nº
                </th>
                <th>
                  Gerencia
                </th>
                <th>
                  DNI
                </th>
                <th>
                  Permiso
                </th>
                <th>
                  EO
                </th>
              </tr>
          </thead>
          <tbody style="text-align: center">
            <tr>
              <td>
                0
              </td>
              <td>
                2134
              </td>
              <td>
                54147890G
              </td>
              <td>
                VE
              </td>
              <td>
                OAFNIE
              </td>
            </tr>
            <tr>
              <td>
                1
              </td>
              <td>
                2534
              </td>
              <td>
                54145820C
              </td>
              <td>
                INVB
              </td>
              <td>
                OAFHIE
              </td>
            </tr>
          </tbody>
        </table>

Nº
盖伦西亚
DNI
佩尔米索
环氧乙烷
0
2134
54147890G
VE
奥夫尼
1.
2534
54145820C
INVB
奥菲
我把我的HTML放在这里,如果你能找到任何线索


试验
window.onload=()=>{
var table=document.getElementById(“myTableTesting”);
var rows=table.getElementsByTagName(“tr”);
for(设i=0;ionClickRowTable(currentRow.rowIndex);
}
}
函数addNewRow(){
设gerencia=document.getElementById(“inputGerencia”).value;
设dni=document.getElementById(“inputDNI”).value;
让permiso=document.getElementById(“inputPermiso”).value;
让eo=document.getElementById(“inputEO”).value;
设numero=-1;
let table=document.getElementById(“myTableTesting”);
var行=table.insertRow(-1);
var cell0=行插入单元格(0);
var cell1=行插入单元格(1);
var cell2=行插入单元格(2);
var cell3=行插入单元格(3);
var cell4=行插入单元格(4);
row.onclick=()=>onClickRowTable(row.rowIndex);
numero=table.rows.length-2;
cell0.innerHTML=numero;
cell1.innerHTML=gerencia;
cell2.innerHTML=dni;
cell3.innerHTML=permiso;
cell4.innerHTML=eo;
//你可以选择在学校里学习
}
函数onClickRowTable(索引){
警报(索引);
log('onClickRowTable',索引);
}
手势
Prueba técnica de la nueva aplicación web

Nº 盖伦西亚 DNI 佩尔米索 环氧乙烷 0 2134 54147890G VE 奥夫尼 1. 2534 54145820C INVB 奥菲
阿尼亚迪尔新坎波。



阿尼亚迪尔
通过使用
var
声明变量,
currentRow
始终包含循环中的最后一个值:

尝试
table.rows[i].rowIndex
而不是
currentRow.rowIndex

或者:用
let
而不是
var
声明变量
currentRow
,这将为变量创建块范围

let currentRow = table.rows[i];

试验
window.onload=()=>{
var table=document.getElementById(“myTableTesting”);
var rows=table.getElementsByTagName(“tr”);
for(设i=0;ionClickRowTable(currentRow.rowIndex);
}
}
函数addNewRow(){
设gerencia=document.getElementById(“inputGerencia”).value;
设dni=document.getElementById(“inputDNI”).value;
让permiso=document.getElementById(“inputPermiso”).value;
让eo=文件。