多行JavaScript表单自动计算

时间:2019-03-21 15:32:25

标签: javascript html

朋友 我正在尝试使报价单自动化,所以我不必计算所有小计等。

我在这里有表格:

function multiply2(element) {
var rowCount=element.id;
var index=rowCount.lastIndexOf("_");
rowCount=rowCount.substring(index+1);

a2 = Number(document.getElementById('quantity_'+rowCount).value);
b2 = Number(document.getElementById('each_'+rowCount).value);
c2 = a2 * b2;
document.getElementById('mc_gross_'+rowCount).value = parseFloat(Math.round(c2 * 100) / 100).toFixed(2);
}
function total20() {
	t1 = Number(document.getElementById('mc_gross_1').value);
	t2 = Number(document.getElementById('mc_gross_2').value);
	sh = Number(document.getElementById('mc_shipping').value);
  tax = Number(document.getElementById('tax').value);
	t21 = t1 + t2 + sh + tax;
	document.getElementById('mc_gross').value = parseFloat(Math.round(t21 * 100) / 100).toFixed(2);
	}
<form method="post" action="">
  <div>
    total: <input type="text" size="4" class="form-control" name="mc_gross" id="mc_gross" onKeyUp="total20()">
   </div> 
   
   <div>
    tax:<input type="text" class="form-control" size="3" name="tax" id="tax" value="10">
    shipping:<input type="text" class="form-control" name="mc_shipping" id="mc_shipping" size="3" value="30">
   </div>
   <br>
   /* row 1 of the data entry */
   <div>
name 1<input class="form-control" type="text" size="8" name="item_name1" id="item_name_1" value="Ipad Air" > 
 qty 1<input class="form-control" type="text" name="quantity1" id="quantity_1" onKeyUp="multiply2(this)" size="2" value="2">
 each 1<input class="form-control" type="text" name="each1" id="each_1" onKeyUp="multiply2(this)" size="2" value="5.00">
 sub total 1<input class="form-control" type="text" size="5" name="mc_gross_1" id="mc_gross_1" readonly>
 </div> <br>
   /* row 2 of the data entry */
   <div>
name 2<input class="form-control" type="text" size="8" name="item_name2" id="item_name_2" value="Ipad Air" > 
 qty 2<input class="form-control" type="text" name="quantity2" id="quantity_2" onKeyUp="multiply2(this)" size="2">
 each 2<input class="form-control" type="text" name="each2" id="each_2" onKeyUp="multiply2(this)" size="2">
 sub total 2<input class="form-control" type="text" size="5" name="mc_gross_2" id="mc_gross_2" readonly>
 </div>
   /* then row 3 - up to number 20 row of the data entry */
</form>

当我报价时-输入名称,数量,每个价格,然后计算每个时间,我得到一行的总数,

然后执行所有需要的行并计算所有小计-然后将运费添加到该数字中,然后计算6%的营业税,我就得出了总计的报价。

如何对所有行进行所有计算是某种循环。

我尝试过类似的操作:Javascript

function multiply1() {
  a1 = Number(document.getElementById('quantity1').value);
  b1 = Number(document.getElementById('each1').value);
  c1 = a1 * b1;
  document.getElementById('mc_gross_1').value = parseFloat(Math.round(c1 * 100) / 100).toFixed(2);
}

function multiply2() {
  a2 = Number(document.getElementById('quantity2').value);
  b2 = Number(document.getElementById('each2').value);
  c2 = a2 * b2;
  document.getElementById('mc_gross_2').value = parseFloat(Math.round(c2 * 100) / 100).toFixed(2);  
}

然后另一个用于求和的函数,但求和函数不起作用,并且所有先前的代码也停止了 感谢您的帮助

谢谢

2 个答案:

答案 0 :(得分:0)

您需要更改以下div:

/* row 2 of the data entry */
<div>
name 2<input class="form-control" type="text" size="8" name="item_name2" id="item_name2" value="Ipad Air" > 
 qty 2<input class="form-control" type="text" name="quantity2" id="quantity2" onKeyUp="multiply2()" size="2">
 each 2<input class="form-control" type="text" name="each2" id="each2" onKeyUp="multiply2()" size="2">
 sub total 2<input class="form-control" type="text" size="5" name="mc_gross_2" id="mc_gross_2" readonly>
 </div>

收件人:

/* row 2 of the data entry */
 <div>
name 2<input class="form-control" type="text" size="8" name="item_name2" id="item_name_2" value="Ipad Air" > 
 qty 2<input class="form-control" type="text" name="quantity2" id="quantity_2" onKeyUp="multiply2(this)" size="2">
 each 2<input class="form-control" type="text" name="each2" id="each_2" onKeyUp="multiply2(this)" size="2">
 sub total 2<input class="form-control" type="text" size="5" name="mc_gross_2" id="mc_gross_2" readonly>
 </div>

这意味着所有ID的格式都为名称 _ 行计数

然后将功能multipli2更改为:

function multiply2(element) {
var rowCount=element.id;
var index=rowCount.lastIndexOf("_");
rowCount=rowCount.substring(index+1);

a2 = Number(document.getElementById('quantity_'+rowCount).value);
b2 = Number(document.getElementById('each_'+rowCount).value);
c2 = a2 * b2;
document.getElementById('mc_gross_'+rowCount).value = parseFloat(Math.round(c2 * 100) / 100).toFixed(2);
}

上面的javascript从id提取行数,然后将行数附加到id字段中以获取目标元素。

答案 1 :(得分:0)

我向您显示小计计算示例代码:

library(readxl) 
library(openxlsx)

wb<-loadWorkbook(choose.files())

data0<-as.data.frame(read_excel(choose.files(),sheet=1,range = "B1:E1131"))
data<-data0
for(i in 1:ncol(data)){
  for(j in 1:nrow(data)){
    if(is.numeric(as.numeric(data[j,i]))&&!is.na(as.numeric(data[j,i]))){
      data[j,i]<-as.numeric(data[j,i])
    }
  }
}