2013-02-11 4 views
0

오른쪽에 3 개의 드롭 다운 상자에 값이 있으며 더 많은 항목이 있습니다 (약 20 개의 드롭 다운 상자를 추가하여 항목의 최종 가격을 제공함). 다음은 HTML 코드입니다 .Javascript가 변수를 함께 추가하지 못합니다.

<form id="myform1> 
<p>Testing 1</p> 
<select id="Test1"> 
<option value="24">Item one</option> 
<option value="29">Item Two<option> 
<!--I have the first box going to 20 items--> 
</select> 

<p>Testing 2</p> 
<select id="Test2"> 
<option value="25">Item one</option> 
<option value="100">Item Two<option> 
<!--I have the second box going to 10 items--> 
</select> 

<p>Testing 3</p> 
<select id="Test3"> 
<option value="24">Item one</option> 
<option value="100">Item Two<option> 
<!--I have the third box going to 10 items--> 
</select> 


</form> 
<button onclick="myFunction()">Update</button> 
<p id="demo"></p> 

그럼 난 내 자바 스크립트가

function myFunction() 
{ 
var a = document.getElementById("list1"); 
var A = a.options[a.selectedIndex].value; 

var b = document.getElementById("list2"); 
var B = b.options[a.selectedIndex].value; 

var c = document.getElementById("list3"); 
var C = c.options[a.selectedIndex].value; 

var IntA = parseInt(A); 
var IntB = parseInt(B); 
var IntC = parseInt(C); 

var x=IntB + IntA; 
var y=x + IntC; 
var demoP=document.getElementById("demo") 
demoP.innerHTML= y; 
} 

내 문제는 내가 완전히 우리의 c를 VAR을 다음 var에 y를 꺼내하고 X로 계산이있는 경우에만 작동합니다 있습니다. 나는 많은 tryed있다 길을 잃어 버렸어. 환호

P. 자바 스크립트는 하단의 스크립트 태그에 있습니다

답변

3

a.selectedIndex을 사용하여 세 요소 모두에 대해 선택된 항목을 가져 왔습니다. 해당 자리에서 b.selectedIndexc.selectedIndex을 사용하도록 변경하십시오. 또는 options 컬렉션을 거치지 않고 a.value, b.valuec.value을 직접 말할 수도 있습니다.

또한 HTML의 id 속성이 JS에서 사용하는 것과 일치하지 않습니다.

는 이러한 문제를 해결하고 당신이 여기에서 볼 수 있듯이 그것은 작동합니다 http://jsfiddle.net/6WWdc/

P.S.을 기수를 IntA = parseInt(A, 10);과 같이 항상 에 두 번째 매개 변수로 전달하는 습관을 들여야합니다. 그렇지 않으면 입력 문자열에 0 또는 선행이 0x 인 경우 (브라우저에 따라 "use strict"; 지시어가 표시됨) 8 진수 또는 16 진수로 해석됩니다.

+0

... 일반적인 기능을 추출하여 반복해서 멈출 수 있습니다. – nzifnab

+0

건배 난 롤에 넣어 후 이름을 알았어 난 그냥 예를 들어 지금 코드를 시도합니다 건배 – user2057440

+0

건배 작품을 사용했다 :) – user2057440

0
function myFunction() 
{ 
    var a = document.getElementById("list1"); 
    var A = a.options[a.selectedIndex].value; 

    var b = document.getElementById("list2"); 
    var B = b.options[b.selectedIndex].value; //changed to var b 

    var c = document.getElementById("list3"); 
    var C = c.options[c.selectedIndex].value; //changed to var c 

    //... 
} 
관련 문제