[중략]
.
============================================================================
자바스크립트로 합계내는 건데...
input 의 value값이 수량이고, itemname이 단가입니다.
totalPrice = 수량(input 값) x 단가(itemname) 값이고, 이 값들을 for 문으로 합산해서 총합계 hap 으로 계산되야 되는데...
값이 표시되지 않고. NaN 이렇게 나오는데...
무엇이 문제인지 못찾고 있습니다.
도움주시면 감사하겠습니다. ^^;;
일단 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>
댓글을 작성하려면 로그인이 필요합니다.
</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='price'> 이런식으로 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>
답변주신 라에님도 감사드립니다. ^^
저렇게도 계산이 되나요?
제가 생각했을땐 input태그를 하나 더 추가해서 name을 itemname으로 하신 후 계산을 하셔야할것같은데요~!
답변을 작성하려면 로그인이 필요합니다.
이 게시물을 신고 하시겠습니까?신고사유를 선택해주세요.