0

我有两种名为“sum”和“sub”的表格。如果我单击“sum”表单的提交按钮,则会调用函数Calculate,类似地,如果我单击“sub”表单的提交按钮,则会调用相同的函数。但我希望在调用“子”形式的计算时不应该执行“总和”形式的方法。我想这可以通过 if 语句来完成,任何人都可以告诉我这样做的语句。

function Calculate() {
 var numsub=document.sub.subnum.value; //e.g: sub form assignment, this should not be executed on calling calculate from "sum" form

 totnumber=totnumber-parseInt(numsub);
 var sp1=document.sub.sp1.value;
 var tot1=sp1*numsub;
 totsellprice +=Math.floor(parseFloat(tot1));
 totnumsell =parseInt(numsub) + totnumsell;
 var number= document.sum.number.value;
 totnumber =parseInt(number) + totnumber;
 var sp= document.sum.sp.value;
 var total= sp*number;
 totprice +=Math.floor(parseFloat(total));
 avgbuy=totprice/totnumber;
 avgsell=totsellprice/totnumsell;
 status= "<h1>Share status</h1>Number of shares: " + totnumber + "</br>total money spent: " + totprice;
 status +="</br>Average Buying price: " + avgbuy;
  status= "</br>Number of shares sold: " + totnumsell + "</br>total money earned: " + totsellprice;
 status +="</br>Average Earning price: " + avgsell;
 document.getElementById('results2').innerHTML= status;
 }
4

2 回答 2

1

function Calculate(type) {
 if(number=="sum")
  {
      //write the logic related to form1
      document.getElementById("sum").submit();
  }else{
      //write the logic related to form2
      document.getElementById("sub").submit();

   }

 }
于 2013-05-18T05:12:59.253 回答
0

在提交时将字符串传递给 javascript 函数。并分别检查 sub 和 sum 形式的字符串值。Calculate('sum'); Calculate('sub')像这样。

于 2013-05-18T05:16:34.497 回答