2016-04-03 8 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フィールドを取得するために使用されます。例えばp_A01 .. calculateTotal()はtotalpriceを計算し、フィールド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

1つだけ変更された場合、 '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

ここでは、すべての入力に1つの関数を使用できます。入力された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();