我有两种形式,名为“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;
}
问题是什么。 –
当我运行这个通过提交“sum”形式它说不能读取未定义的属性subnum。 subnum是“sub”形式的属性,我不希望它在提交“sum”形式时被读取。 – user2394553