2013-05-18 31 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')是這樣的。

相關問題