我想使用javascript添加工资列中的数据 [英] I want to add the data present in salary column by using javascript
问题描述
在我的代码中我收到错误
In my code I am getting the error "
Uncaught TypeError: Cannot read property 'cells' of undefined
我的代码如下:
<!DOCTYPE html>
< html xmlns =http://www.w3.org/1999/xhtml >
< head>
< title>< / title>
< script type =text / javascript >
函数fnsum(){
调试器;
var otbtab = document.getElementById('tab1')
var val = 0;
var n = 0;
for(var i = 1; i< otbtab.rows [i] .cells [2]。 innerHTML.length; i ++){
val = parseInt(otbtab.rows [i] .cells [2] .innerHTML)+ val;
}
}
//非常规的工作方式很好
// var v1 = otbtab.rows [1] .cells [2] .innerHTML;
// var v2 = otbtab.rows [2] .cells [2] .innerHTML;
// var v3 = otbtab.rows [ 3] .cells [2] .innerHTML;
// var result = parseInt(v1)+ parseInt(v2)+ parseInt(v3);
// alert( 'SALARY是'+结果);
< / script>
< / head>
< body>
EID | NAME | SALARY |
---|---|---|
Mahesh | 1800 | |
2 | Suresh | 21000 |
3 | Veena | 24000 |
< input type =submitvalue =DISPLAYTOTALSALAR Yid =btnsubmitonclick =fnsum()/>
< / body>
< / html>
"
My code is as follows:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function fnsum() {
debugger;
var otbtab = document.getElementById('tab1')
var val = 0;
var n = 0;
for (var i = 1; i < otbtab.rows[i].cells[2].innerHTML.length; i++) {
val = parseInt(otbtab.rows[i].cells[2].innerHTML) + val;
}
}
//NON GENERIC WAY IS WORKING FINE
//var v1 = otbtab.rows[1].cells[2].innerHTML;
//var v2 = otbtab.rows[2].cells[2].innerHTML;
//var v3 = otbtab.rows[3].cells[2].innerHTML;
//var result = parseInt(v1) + parseInt(v2) + parseInt(v3);
//alert('SUM OF SALARY IS'+result);
</script>
</head>
<body>
EID | NAME | SALARY |
---|---|---|
1 | Mahesh | 1800 |
2 | Suresh | 21000 |
3 | Veena | 24000 |
<input type="submit" value="DISPLAYTOTALSALARY" id="btnsubmit" onclick="fnsum()" />
</body>
</html>
推荐答案
数组的索引通常从0到*。长度 - 1;
不从1到* .length;
修复它;这个问题将会发生。
在JavaScript中,您可以尝试使用任何索引对象索引对象,但返回值可以是undefined
;如果您从未在此索引处分配任何值,则会发生这种情况您还需要了解索引值和属性是一回事; JavaScript对象是关联数组:
http:// en。 wikipedia.org/wiki/JavaScript#Dynamic [ ^ ],
http://en.wikipedia.org/wiki/Associative_array [ ^ ]。
JavaScript中的数组和其他对象类型对象只有一个区别:数组中的属性length
。
-SA
Arrays are normally indexed from 0 to*.length − 1;
not from 1 to*.length;
fix it; and this problem will go.
In JavaScript, you can try to index an object with any index object, but the return can beundefined
; it happens if you never assigned any value at this index. You also need to understand that indexed values and properties is the same thing; JavaScript object is the associative array:
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^],
http://en.wikipedia.org/wiki/Associative_array[^].
Arrays and other 'object'-type objects in JavaScript have only one difference: propertylength
in arrays.
—SA
这篇关于我想使用javascript添加工资列中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!