2013-03-05 2 views
-3

은이 같은, JS에서 간단한 승산을 계산하기 위해 시도하고있다 : =
A * B = C
위해 D * E = F
G의 *의 H I
총계 = C + F + IHTML 페이지의 값을 사용하여 JavaScript에서 값을 계산하는 방법은 무엇입니까?

그러나 총합은 예상보다 훨씬 큽니다. 곱셈에 문제가 있거나 페이지에서 값을 잘못 읽었습니까?

<html> 
<head> 
<script> 
    calculate = function(totalElement) 
    { 
     if (totalElement) 
     { 
      var calculation = ''; 
      var overall = ''; 
      var fields = new Array(); 
      var theElement = document.getElementById(totalElement); 
      var userInputs = myform.elements; 
      var the_type = ''; 
      for (var f = 0; f < userInputs.length; f++) 
      { 
       if (userInputs[f].className=='special_value') 
       { 
       if(userInputs[f].value != "") 
       { 
        document.getElementById("price4").value+=+userInputs[f].value; 

       }   

       } 
      } 


     } 
    } 
</script> 
</head> 
<body> 
<form name="myform"> 
A<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" value="" type="text" name="price"> 
B<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" value="" name="price2"> 
C<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text"value="" name="price4" id="price4"> <Br/> 
D<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" value="" type="text" name="price5"> 
E<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" value="" name="price6"> 
F<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text"value="" name="price7" id="price7"> <Br/> 
Total<input class="special_value" value="" id="total" type="text" name="total"> <Br/> 
<!-- <div id="total"></div> --> 
<input onClick="calculate('total');" type="button" name="button" value="re-calculate"> 
</form> 
</body> 
</html> 
+2

* 그러나, 값이 날 너무 큰거야 * 그래서 질문은 무엇인가.? – VisioN

+0

여기에 무슨 일이 ... document.getElementById ("price4"). value + = + userInputs [f] .value; .. + = + ... 흠 .. –

+3

이것은 잠시 읽은 것보다 덜 이해합니다. 'A * B = C D * E = F G * H = I 총계 = C + F + I'. 이 질문은 일반적으로 의미가 없습니다. – jahroy

답변

5

document.getElementById("price4").value은 문자열입니다

다음은 내 코드입니다. 문자열을 연결하는 것보다는 문자열을 연결하는 것이 좋습니다.

시도 : parseFloat(document.getElementById("price4").value)

+0

고맙습니다. 자동으로 숫자를 곱하고 값을 할당 할 수 있도록 만드는 방법이 있습니까? – NULL

+1

예, 자바 스크립트를 사용하십시오. –

+0

감사합니다 Diodeus, 아주 간단한 계산, 나는 주요 문제가 문자열 연결 것 같아요. 간과 할 수있는 무엇인가! – NULL

관련 문제