[英]How to calculate in html table rows using javascript
This is my table image that i have fetched from the another table in this table i am doing some calculation using javascript.When i tried to change the qty value,it should multiply with rate and show it in the total.这是我从该表的另一个表中获取的表图像,我正在使用 javascript 进行一些计算。当我尝试更改数量值时,它应该乘以比率并显示在总数中。 My problem when i change the value the both rows of the total shows same result.
当我更改值时,我的问题是总计的两行都显示相同的结果。
My code:我的代码:
<script type="text/javascript">
function calculate(id){
var tot = 0;
var qty = document.getElementsByClassName("qty");
var rate = document.getElementsByClassName("rate");
var total = document.getElementsByClassName("total");
for(var i = 0; i < qty.length; i++)
{
tot = parseFloat(qty[i].value) * parseFloat(rate[i].value);
$(".total").val(tot);
}
}
</script>
Well of course it is.当然是的。
$(".total").val(tot);
modifies EVERY element of class total.修改 class 的每个元素。 Try
$(".total").eq(i).val(tot);
试试
$(".total").eq(i).val(tot);
Note: this solution won't work if you have other .total
elements on the page, outside of the table you're working on.注意:如果页面上有其他
.total
元素,在您正在处理的表格之外,此解决方案将不起作用。 You'll have to select the table first, $('table#tableid.total').eq(i)...
to overcome that issue你必须先 select 表,
$('table#tableid.total').eq(i)...
来克服这个问题
It does it cause you did not specify which which row specifically should be updated with the following line of code:这样做会导致您没有指定应该使用以下代码行具体更新哪一行:
$(".total").val(tot);
What you do instead is updating values of all the fields within the class total.您所做的是更新 class 总计中所有字段的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.