请原谅我,因为我处于网络开发的早期阶段。在这里,我想从系列行中得到总和(总和),如下所示。请帮我生成用于计算Amount列的每一行的代码。我尝试了所有的方法,包括stackoverflow和quora中的其他答案,但没有用,并留在混乱的海洋。在这方面的任何帮助将非常感谢(评论或小提琴或答案)
<table>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Rate</th>
<th>Amount</th>
</tr>
<tr>
<td>Steel</td>
<td><input id="qty1" type="text" oninput="cal1()" /></td>
<td><input id="rate1" type="text" oninput="cal1()" /></td>
<td><input id="amt1" type="text" /></td>
</tr>
<tr>
<td>Cement</td>
<td><input id="qty2" type="text" oninput="cal2()" /></td>
<td><input id="rate2" type="text" oninput="cal2()" /></td>
<td><input id="amt2" type="text" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>TOTAL<input id="tot1" type="text" /></td>
//Here I want to know how to get the total sum of amt1(id) and amt2 (id) automatically
</tr>
</table>
<script type="text/javascript">
function cal1(){
var x1 = document.getElementById('qty1').value;
var x2 = document.getElementById('rate1').value;
var x3 = document.getElementById('amt1').value;
var x4 = x1 * x2;
amt1.value = x4;
}
function cal2(){
var y1 = document.getElementById('qty2').value;
var y2 = document.getElementById('rate2').value;
var y3 = document.getElementById('amt2').value;
var y4 = y1 * y2;
amt2.value = y4;
}
//Here I want to know the sum of id(amt1) and (amt2)
</script>
我有计算&#34; Amount&#34;的每一行的输出。柱。但是,无法获取此链接中的金额列总数 - http://tinypic.com/view.php?pic=kales0&s=9
答案 0 :(得分:0)
查看codepen.io上的基本示例,以帮助您入门。
本质上你的cal1和cal2函数没有按照你的意图行事,而且你可能还想要另一个计算总数的函数,每次amt1或amt2值改变时。
Javascript代码:
function cal1() {
var x1 = document.getElementById('qty1');
var x2 = document.getElementById('rate1');
var x3 = document.getElementById('amt1');
x3.value = parseInt(x1.value) + parseInt(x2.value);
calcTotal();
}
function cal2() {
var y1 = document.getElementById('qty2');
var y2 = document.getElementById('rate2');
var y3 = document.getElementById('amt2');
y3.value = parseInt(y1.value) + parseInt(y2.value);
calcTotal();
}
function calcTotal() {
var amt1 = document.getElementById('amt1');
var amt2 = document.getElementById('amt2');
var tot1 = document.getElementById('tot1');
tot1.value = parseInt(amt1.value) + parseInt(amt2.value);
}
HTML code:
<html>
<head>
<title>calculation exercise</title>
</head>
<body>
<table>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Rate</th>
<th>Amount</th>
</tr>
<tr>
<td>Steel</td>
<td><input id="qty1" type="text" oninput="cal1()" /></td>
<td><input id="rate1" type="text" oninput="cal1()" /></td>
<td><input id="amt1" type="text" /></td>
</tr>
<tr>
<td>Cement</td>
<td><input id="qty2" type="text" oninput="cal2()" /></td>
<td><input id="rate2" type="text" oninput="cal2()" /></td>
<td><input id="amt2" type="text" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>TOTAL<input id="tot1" type="text" /></td>
</tr>
</table>
</body>