2016-04-03 37 views
-1

是否可以像數組一樣創建函數? ,以便以下功能可以變得簡單並易於編輯。製作一個javascript數組函數

function q1() { 
    var theForm = document.forms["contact-form"]; 
    var quantity = theForm.elements["q1"]; 
    var howmany = 0; 
    if (quantity.value != "") { 
    howmany = parseInt(quantity.value); 
    } 
    return howmany; 
} 

function q2() { 
    var theForm = document.forms["contact-form"]; 
    var quantity = theForm.elements["q2"]; 
    var howmany = 0; 
    if (quantity.value != "") { 
    howmany = parseInt(quantity.value); 
    } 
    return howmany; 
} 

function q3() { 
    var theForm = document.forms["contact-form"]; 
    var quantity = theForm.elements["q3"]; 
    var howmany = 0; 
    if (quantity.value != "") { 
    howmany = parseInt(quantity.value); 
    } 
    return howmany; 
} 

現在我改變了這樣的 GetQuantity()用於獲取從數量字段中的值。例如q_A01 .. GetPrice()用於獲取只讀值Price字段。例如calculate_total()用於計算總價格並返回字段ID「Total」。p_A01 .. calculateTotal()用於計算總價格並返回字段ID「Total」。

function GetQuantity(e) { 
var theForm = document.forms["contact-form"]; 
var quantity = theForm.elements[e]; 
var howmany =0; 
if(quantity.value!=0) { 
howmany = parseInt(quantity.value); } 
return howmany; 
} 
function GetPrice(e) { 
    var theForm = document.forms["contact-form"]; 
    var price = theForm.elements[e]; 
    var howmany =0; 
    if(price.value!=0) { 
    howmany = parseInt(price.value); } 
    return howmany; 
    } 

function calculateTotal() 
{ 
    var cakePrice = 
GetPrice(p_A01)*GetQuantity(q_A01)+ 
GetPrice(p_A02)*GetQuantity(q_A02)+ 
GetPrice(p_A03)*GetQuantity(q_A03)+ 
GetPrice(p_F11)*GetQuantity(q_F11); 

    var Totalordered = document.getElementById ("Total"); 
     Totalordered.value = cakePrice; 


} 
+0

你當然可以寫得更短,也許只是'function fetch(){return 0}',因爲它們都返回相同的東西嗎? – adeneo

+0

當只有一件事情發生變化時,一個「switch」或甚至一個對象查找表會更合適。 – dandavis

+0

我想你的函數應該返回'return quantity.length'而不是'howmany' – RomanPerekhrest

回答

1

不知道爲什麼沒有答案的建議明顯的 - 保持在函數體的公共代碼,並做出改變的代碼的函數參數:

function q(e) { 
    var theForm = document.forms["contact-form"]; 
    var quantity = theForm.elements[e]; 
    var howmany = 0; 
    if (quantity.value != "") { 
    howmany = parseInt(quantity.value); 
    } 
    return howmany; 
} 

或較短的版本,幾乎與原來的一樣:

function q(e) { 
    var quantity = document.forms["contact-form"].elements[e]; 
    return (quantity && quantity.value) || 0; 
} 
0

你所有的功能都做同樣的事情。就在不同的數據傳遞到一個單一的功能:

function q1(qty) { 
    var theForm = document.forms["contact-form"]; 
    var howmany = 0; 
    // Do whatever you need to with the qty argument. 
    return howmany; 
} 

但有趣的是,有一兩件事改變(數量)是東西你的職責實際上並不做任何事情。

這是任何編程語言中函數的基本前提,也是DRY一般最佳實踐原理的基礎(不要重複自己)。由於你所有的功能都做同樣的事情,只需要改變(數量)並將其分開即可。現在,你沒有5個功能來存儲和維護。

+0

非常感謝所有人回答我的問題。我錯過了一個劇本。對於那個很抱歉。現在更新。 –

0

在這裏,您可以對所有輸入使用一個函數,只需傳入輸入ID或名稱即可獲取該元素的值。

var q = 'q2'; 
 
function qty(q1) { 
 
     var theForm = document.forms["contact-form"]; 
 
     var quantity = theForm.elements[q1]; var howmany =0; 
 
    return howmany; 
 
} 
 
    document.write(qty(q));

+0

現在明白了。 –

+0

感謝Hitesh,感謝每一個人,謝謝你的歡迎,stackoverflow –

+0

。 。 –

0
Hi check the following code as you updated 
    function GetQuantity(e) { 
     var theForm = document.forms["contact-form"]; 
     var quantity = theForm.elements[e]; 
     var howmany =0; 
     if(quantity.value!=0) { 
     howmany = parseInt(quantity.value); } 
     return howmany; 
    } 
    function GetPrice(e) { 
     var theForm = document.forms["contact-form"]; 
     var price = theForm.elements[e]; 
     var howmany =0; 
     if(price.value!=0) { 
     howmany = parseInt(price.value); } 
     return howmany; 
     } 

    function calculateTotal() 
    { 
     var cakePrice = 
    GetPrice('p_A01')*GetQuantity('q_A01')+ 
    GetPrice('p_A02')*GetQuantity('q_A02')+ 
    GetPrice('p_A03')*GetQuantity('q_A03')+ 
    GetPrice('p_F11')*GetQuantity('q_F11'); 

     var Totalordered = document.getElementById ("Total"); 
      Totalordered.value = cakePrice; 


    } 
      calculateTotal(); 
相關問題