[英]My Javascript code not working properly
I am creating a simple function that increment number and bind with multiple Table as S.NO. 我正在创建一个简单的函数,该函数递增数字并与多个表绑定为S.NO。 . 。 I don't understand what's wrong with my code. 我不明白我的代码有什么问题。
function _IncrementNumber(id) {
var table = document.getElementById(id);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
table.rows[i + 1].cells[0].innerHTML = (i + 1);
}
}
$(document).ready(function () {
_IncrementNumber("FirstTable");
_IncrementNumber("SecondTable");
});
You are accessing i+1 instead of i . 您正在访问i + 1而不是i 。
In the last iteration -> you will go out of bounds. 在最后一次迭代->中,您将越界。
function _IncrementNumber(id) {
var table = document.getElementById(id);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
table.rows[i].cells[0].innerHTML = (i + 1);
}
}
$(document).ready(function () {
_IncrementNumber("FirstTable");
_IncrementNumber("SecondTable");
});
You're trying to access a row that doesnt exist. 您正在尝试访问不存在的行。
You should start with i=1 instead to skip the header row 您应该以i = 1开头,而不要跳过标题行
for (var i = 1; i < rowCount; i++) {
table.rows[i].cells[0].innerHTML = (i);
}
the error at this line was the issue. 这行的错误是问题所在。 Table rows starts from 0 index. 表行从0索引开始。 so dont need to increment the rows index change to this table.rows[i].cells[0].innerHTML = (i + 1);
因此不需要增加对该表的行索引更改table.rows[i].cells[0].innerHTML = (i + 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.