2017-11-28 8 views
0

私はJavaScriptの相対的な新人ですので、私の用語のいくつかが間違っている場合は、私に同行してください。私は以下のコードと関数を持っており、関数の実行結果を関数AddToCartのarameterに渡す必要があります。私は両方の関数を独立して動作させることができ、id = "resultPrintValue"は常に正しい数値を返します。基本的には、その値をAddToCartパラメータにどのように取得するのですか?私は同様の質問を見てきましたが、パラメータ内に関数を置くなどいくつかの提案を試みましたが、すべてNot An Number(NaN)を返します - 私は何か不足しているようです。選択したオプション値を新しい関数のパラメータに渡す方法

<!-- select option - calls function run onchange --> 

<select name="Print" id="Print" onchange="run()" > 
<option value="0">select print size </option>   
<option value="1">Lush Tapestry Print on Paper 50cm x 25cm £125</option> 
<option value="2">Another print £25</option> 
<option value="3">he original £4000000</option> 
<select> 

<!-- code below returns the value (0-3) correctly of selected option. Need to pass this result in function AddToCart --> 

<p>Your id is: </p><p id="resultPrintValue"> </p> 


<!-- Stuck with code to put value in parameter of function AddToCart - If I hard code a number 0-3 then AddToCart works fine --> 


functions are as below 

function run() { 
document.getElementById("resultPrintValue").innerHTML = document.getElementById("Print").value; 
return (resultPrintValue) 
} 

function AddToCart(x) 
{ 
var product_index = cart_products.indexOf(x); 
if (product_index > -1) 
{ 
cart_quantities[x]++; 
} 
else 
{ 
cart_products.push(x); 
cart_quantities[x] = 1; 
} 
ShowPopup("The print was added to your basket!"); 
UpdateCart(); 
} 

答えて

0

パラメータとして渡す必要はありません。変数を実行関数の戻り値と同じに設定するだけです。

function AddToCart() 
{ 
var x = this.run() 
... 
} 
0
 var cart_quantities = []; 
     var cart_products = []; 
     function run() { 
      var value = parseInt(document.getElementById("Print").value, 10); 
      document.getElementById("resultPrintValue").innerHTML = value; 
      AddToCart(value); 
     } 
     function AddToCart(x) { 
      var product_index = cart_products.indexOf(x); 
      if (product_index === -1) { 
       cart_products.push(x); 
      } 

      ShowPopup("The print was added to your basket!"); 
      UpdateCart(); 
     } 
+0

コードだけでなく説明が必要 –

関連する問題