2017-11-28 4 views
0

사전 도움 주셔서 감사합니다. 저는 자바 스크립트의 상대적인 신참입니다. 따라서 제 용어가 잘못되어 있으면 제발 참아주십시오. 나는 아래의 코드와 함수를 가지고 있고, 함수의 결과를 함수 AddToCart의 arameter에 전달할 필요가있다. 독립적으로 작동하는 두 함수를 얻을 수 있으며 id = "resultPrintValue"는 항상 올바른 숫자 값을 반환합니다. 기본적으로 어떻게 그 값을 AddToCart 매개 변수로 가져 옵니까? 나는 비슷한 질문을 보았고 매개 변수 안에 함수를 넣는 등의 몇 가지 제안을 시도했지만 모두 숫자가 아닌 (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

매개 변수로 전달할 필요는 없습니다. run 함수의 반환 값과 동일한 변수를 설정하기 만하면됩니다.

function AddToCart() 
{ 
var x = this.run() 
... 
} 
+0

사과는 내가 두 번째 함수 – peternet

+0

OK 나는 href="javascript:AddToCart();"> (추가 버튼을 peternet

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

코드뿐만 아니라 설명이 필요하다. –

관련 문제