2014-07-27 17 views
0

나는 항목, 가격, 수량별 가격 등을 표시해야하는 연습이 있습니다 ... 수량에 가격을 곱하는 데 어려움이 있습니다. .. 아래에 내가 표시하고 입력과 옵션 내 코드이며, 아래의 스크립트 기능은 도움이 아주 많이 감사합니다! :두 변수 (JS)를 입력에 곱하기

<body> 
<label>Choose Item</label> 
<select onChange="getPrice(this)"> 
    <option></option> 
    <option>Tomatoes</option> 
    <option>Lettuce</option> 
    <option>Potato</option> 
    <option>Carrots</option> 
    <option>Artichoke</option> 
</select> 
<br/> 
<label>Price</label> 
    <input type="text" id="price" /> 
<br/> 
<label>Quantity</label> 
<select onChange="getFull(this)"> 
    <option></option> 
    <option>1</option> 
    <option>2</option> 
    <option>3</option> 
    <option>4</option> 
    <option>5</option> 
</select> 
<br /> 
<label>Total $</label> 
    <input type="text" id="quantityPrice" /> 

<script type="text/javascript"> 
    var veggy = {}; 
    veggy ["Tomatoes"] = 5.99 
    veggy ["Lettuce"] = 7.66 
    veggy ["Potato"] = 4.52 
    veggy ["Carrots"] = 2.13 
    veggy ["Artichoke"] = 10.58 

function getPrice(select) { 
    document.getElementById("price").value = veggy[select.value]; 
} 

var quantity = {}; 
    quantity ["1"] = 1 
    quantity ["2"] = 2 
    quantity ["3"] = 3 
    quantity ["4"] = 4 
    quantity ["5"] = 5 

function getFull(select) { 
    document.getElementById("quantityPrice").value = (quantity[select.value] * veggy [select.value]); 
} 
</script> 
</body> 
</html> 
+0

같은

<select id="veggy" onChange="getPrice(this)"> 

그리고 getFull, 그 시도. – Mritunjay

답변

0

문제가있는 이유는 키를 가진 veggy 개체에서 값을 가져 오려고하기 때문입니다. 여기

function getFull(select) { 
    document.getElementById("quantityPrice").value = (quantity[select.value] * veggy [select.value]); 
} 

당신이 quanntity의 참조를 전달하는 선택,하지만 당신은 price 형태 Item 개체를 얻을 수 있습니다.

Id을 먼저 ChooseItems에 select으로 지정하십시오. 내가 대답을 추가 한이

function getFull(select) { 
    var vegSelect = document.getElementById('veggy'); 
    document.getElementById("quantityPrice").value = (quantity[select.value] * (veggy[vegSelect.value])); 
} 
+0

작동합니다! 대단히 감사합니다! 내가 왜 var veg select를 추가했는지 이해할 수 없다. – Yaelix

+0

@Yaelix 아이템 선택 상자가 선택된 값을 얻는다. – Mritunjay

0

값에 문자열로 숫자를 변환

document.getElementById("quantityPrice").value =''+ (quantity[select.value] * veggy [select.value]);