테스트 사이트 - 개발 중인 베타 버전입니다

자바스크립트 합계구하기 채택완료

구름빵 8년 전 조회 10,055

[중략]

    

                                                          

.

.

.

.

   

 

         

============================================================================

자바스크립트로 합계내는 건데...

 

input 의 value값이 수량이고, itemname이 단가입니다.

 

totalPrice = 수량(input 값) x 단가(itemname) 값이고, 이 값들을 for 문으로 합산해서 총합계 hap 으로 계산되야 되는데...

값이 표시되지 않고. NaN 이렇게 나오는데...

무엇이 문제인지 못찾고 있습니다.

 

도움주시면 감사하겠습니다. ^^;; 

댓글을 작성하려면 로그인이 필요합니다.

답변 4개

채택된 답변
+20 포인트
8년 전

일단 id가 두개네요

id="price"를 클래스로 변경하시고

 

가격 구하는 함수를 변경하시면 됩니다. 

</p><p> </p><p>function addPrice() {
 totalPrice = 0;

    
$(".price").each(function(){
    totalPrice += Number($(this).val()) * Number($(this).attr("itemname"));
});

 document.getElementById('hap').value = comma(totalPrice);
 document.getElementById('to_price').innerText = comma(parseInt(totalPrice) + (parseInt(totalPrice)/10));
 document.getElementById('총결제비용').value = parseInt(totalPrice) + (parseInt(totalPrice)/10); 
} </p><p> </p><p><input type=text  class="price" name="wr_11" onblur="addPrice()" size="5" maxlength="5" id="wr_11"  itemname="20"  value=""> </p><p>
 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

IZSOFT
8년 전

</p><p>function addPrice() {</p><p> var totalPrice = 0;</p><p> var price = document.getElementsByClassName('price');</p><p> for(i=0; i<price.length; i++) {</p><p>  {</p><p><span class="Apple-tab-span" style="white-space:pre">			</span>totalPrice += Number(price[i].value) * Number(price[i].getAttribute('itemname'));</p><p>  }</p><p> }</p><p> document.getElementById('hap').value = comma(totalPrice); </p><p> document.getElementById('to_price').innerText = comma(parseInt(totalPrice) + (parseInt(totalPrice)/10));</p><p> document.getElementById('총결제비용').value = parseInt(totalPrice) + (parseInt(totalPrice)/10); </p><p>}</p><p>

 

일단 jquery는 잘 모르는 것 같아서 님 수준에 맞게 수정해본거구요. id는 중복해서 쓰면 안됩니다.

그래서 가격 입력받는곳에

이런식으로 class를 같게 맞춰줍니다. 그리고 님 html 보면 input에 id가 두번 선언되어있어요.

</p><p>function addPrice() {</p><p> var totalPrice = 0;</p><p> $('.price').each(function() {</p><p><span class="Apple-tab-span" style="white-space:pre">	</span>totalPrice += Number($(this).val()) * Number($(this).attr("itemname"));</p><p> });</p><p> </p><p> $('#hap').val(comma(totalPrice)); </p><p> $('#to_price').val(comma(Number(totalPrice) + (Number(totalPrice)/10)));</p><p>}</p><p>

이건 jQuery로 짠거고 
   
   
항목 1 / 가  격: 20원수 량:                
항복2 / 가  격: 30원수 량: 

           

합계금액
총 결제 비용0 원 (부가세가 포함된 금액입니다.)     
총 결제 비용 원 (부가세가 포함된 금액입니다.)
총합부분도 span으로 찍어주는것으로 해놨는데 그럴필요가 없어요. input에 border 값 없애고 우측 정렬하면 input인지 티가 안남 readonly 속성때문에 수정 안되구요
로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

8년 전

답변주신 라에님도 감사드립니다. ^^ 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

8년 전

저렇게도 계산이 되나요?

제가 생각했을땐 input태그를 하나 더 추가해서 name을 itemname으로 하신 후 계산을 하셔야할것같은데요~!

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인

© 2025 SIRSOFT. All rights reserved.