2013-05-18 26 views
0

我有两种形式,名为“sum”和“sub”。如果我点击“sum”形式的提交按钮,函数Calculate会被调用,同样如果我点击“sub”形式的提交按钮,调用相同的函数。但我希望“sum”形式的方法不应该在调用“sub”形式的计算时执行。我想这可以通过if语句完成,任何人都可以告诉我这样做的声明。通过单个javascript函数处理两种形式

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; 
} 
+0

问题是什么。 –

+0

当我运行这个通过提交“sum”形式它说不能读取未定义的属性subnum。 subnum是“sub”形式的属性,我不希望它在提交“sum”形式时被读取。 – user2394553

回答

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(); 

    } 

} 
+0

什么是'第一'?你的'if'语法看起来无效。 – Barmar

+0

对不起。这是错字 – PSR

+0

@Barmar我更新了我的代码 – PSR

0

将字符串传递对提交的JavaScript功能。并分别检查sub和sum形式的字符串值。 Calculate('sum'); Calculate('sub')是这样的。

相关问题