합계 구하기 도와 주세요^.^
본문
안녕하세요? 계산식을 이용해서 매장에서 사용하는 계산기를 만들어 보려 합니다
현재
<!-- 지출 -->
<script type="text/javascript">
$(function () {
$('.subtot, .grdtot').prop('readonly', true);
var $tblrows = $("#tblProducts tbody tr");
$tblrows.each(function (index) {
var $tblrow = $(this);
$tblrow.find('.qty, .price').on('change', function () {
var qty = $tblrow.find("[name=qty]").val();
var price = $tblrow.find("[name=price]").val();
var subTotal = parseInt(qty, 10) * parseFloat(price);
if (!isNaN(subTotal)) {
$tblrow.find('.subtot').val(subTotal.toFixed(0));
var grandTotal = 0;
$(".subtot").each(function () {
var stval = parseFloat($(this).val());
grandTotal += isNaN(stval) ? 0 : stval;
});
$('.grdtot').val(number_format(grandTotal.toFixed(0)));
}
});
});
});
</script>
<div class="table-responsive">
<table id="tblProducts" class="table table-striped">
<thead>
<tr style="text-align: center;">
<td>구분</td>
<td>금액</td>
<td>수량</td>
<td>합계</td>
</tr>
</thead>
<tbody style="text-align: center;">
<tr>
<td>전기</td>
<td><input type="text" class="qty" value="" name="qty"/></td>
<td><input type="text" class="price" value="1" name="price"/></td>
<td><input type="text" class="subtot" value="0" name="subtot"/></td>
</tr>
<tr>
<td>가스</td>
<td><input type="text" class="qty" value="" name="qty"/></td>
<td><input type="text" class="price" value="1" name="price"/></td>
<td><input type="text" class="subtot" value="0" name="subtot"/></td>
</tr>
<tr>
<td>수도</td>
<td><input type="text" class="qty" value="" name="qty"/></td>
<td><input type="text" class="price" value="1" name="price"/></td>
<td><input type="text" class="subtot" value="0" name="subtot"/></td>
</tr>
<tr>
<td>인터넷</td>
<td><input type="text" class="qty" value="" name="qty"/></td>
<td><input type="text" class="price" value="1" name="price"/></td>
<td><input type="text" class="subtot" value="0" name="subtot"/></td>
</tr>
<tr>
<td>포스</td>
<td><input type="text" class="qty" value="" name="qty"/></td>
<td><input type="text" class="price" value="1" name="price"/></td>
<td><input type="text" class="subtot" value="0" name="subtot"/></td>
</tr>
<tr>
<td>정수기</td>
<td><input type="text" class="qty" value="" name="qty"/></td>
<td><input type="text" class="price" value="1" name="price"/></td>
<td><input type="text" class="subtot" value="0" name="subtot"/></td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td style="text-align: center;"> 예상(지출):<p><input type="text" class="grdtot" value="" name=""/></td>
</tr>
</tfoot>
</table>
</div>
<!-- 수입 -->
<script type="text/javascript">
$(function () {
$('.subtot1, .grdtot1').prop('readonly', true);
var $tblrows = $("#tblProducts tbody tr");
$tblrows.each(function (index) {
var $tblrow = $(this);
$tblrow.find('.qty1, .price1').on('change', function () {
var qty1 = $tblrow.find("[name=qty1]").val();
var price1 = $tblrow.find("[name=price1]").val();
var subTotal1 = parseInt(qty1, 10) * parseFloat(price1);
if (!isNaN(subTotal1)) {
$tblrow.find('.subtot1').val(subTotal1.toFixed(0));
var grandTotal1 = 0;
$(".subtot1").each(function () {
var stval = parseFloat($(this).val());
grandTotal1 += isNaN(stval) ? 0 : stval;
});
$('.grdtot1').val(number_format(grandTotal1.toFixed(0)));
}
});
});
});
</script>
<div class="table-responsive">
<table id="tblProducts" class="table table-striped">
<thead>
<tr style="text-align: center;">
<td>구분</td>
<td>금액</td>
<td>수량</td>
<td>합계</td>
</tr>
</thead>
<tbody style="text-align: center;">
<tr>
<td>전기</td>
<td><input type="text" class="qty1" value="" name="qty1"/></td>
<td><input type="text" class="price1" value="1" name="price1"/></td>
<td><input type="text" class="subtot1" value="0" name="subtot1"/></td>
</tr>
<tr>
<td>가스</td>
<td><input type="text" class="qty1" value="" name="qty1"/></td>
<td><input type="text" class="price1" value="1" name="price1"/></td>
<td><input type="text" class="subtot1" value="0" name="subtot1"/></td>
</tr>
<tr>
<td>수도</td>
<td><input type="text" class="qty1" value="" name="qty1"/></td>
<td><input type="text" class="price1" value="1" name="price1"/></td>
<td><input type="text" class="subtot1" value="0" name="subtot1"/></td>
</tr>
<tr>
<td>인터넷</td>
<td><input type="text" class="qty1" value="" name="qty1"/></td>
<td><input type="text" class="price1" value="1" name="price1"/></td>
<td><input type="text" class="subtot1" value="0" name="subtot1"/></td>
</tr>
<tr>
<td>포스</td>
<td><input type="text" class="qty1" value="" name="qty1"/></td>
<td><input type="text" class="price1" value="1" name="price1"/></td>
<td><input type="text" class="subtot1" value="0" name="subtot1"/></td>
</tr>
<tr>
<td>정수기</td>
<td><input type="text" class="qty1" value="" name="qty1"/></td>
<td><input type="text" class="price1" value="1" name="price1"/></td>
<td><input type="text" class="subtot1" value="0" name="subtot1"/></td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td style="text-align: center;"> 예상(수입):<p><input type="text" class="grdtot1" value="" name=""/></td>
</tr>
</tfoot>
</table>
</div>
<!--전체-->
이렇게 구성을 하여 수입 부분과 지출 부분을 입력받게 만들어 보았습니다~
질문1 : 예상(수입) - 예상(지출) 값 구하기
질문2: 금액 x 수량 값을 구하는 부분에서
금액을 100원 입략하고 수량 1개를 입력하면 결과값이 100이 나옵니다
그런데 문제는 100원을 지우면 값이 그대로 100으로 남아 있습니다.
0을 입력 하면 값이 0으로 돌아 옵니다...
이부분을 입력값이 없을때 자동으로 값에 반영을 시키려면?
도와 주세요^.^
!-->
답변 2
결과를 보여줍니다
<div id="margin"></div>
스크립트 상단하단
//결과 계산 margin 계산
function grand_margin(){
var grdtot=$(".grdtot").val();
var grdtot1=$(".grdtot1").val();
var margin=0;
if(!isNaN(grdtot) && !isNaN(grdtot1)){
margin=parseFloat(grdtot1)-parseFloat(grdtot);
}
jQuery("#margin").empty().append(number_format(magin.toFixed(0)))
}
$(function () {
$('.subtot, .grdtot').prop('readonly', true);
var $tblrows = $("#tblProducts tbody tr");
$tblrows.each(function (index) {
var $tblrow = $(this);
$tblrow.find('.qty, .price').on('change', function () {
var qty = $tblrow.find("[name=qty]").val();
var price = $tblrow.find("[name=price]").val();
var subTotal = parseInt(qty, 10) * parseFloat(price);
if (!isNaN(subTotal)) {//2번문제
$tblrow.find('.subtot').val(subTotal.toFixed(0));
}
else{
$tblrow.find('.subtot').val(0);
}
var grandTotal = 0;
$(".subtot").each(function () {
var stval = parseFloat($(this).val());
grandTotal += isNaN(stval) ? 0 : stval;
});
$('.grdtot').val(number_format(grandTotal.toFixed(0)));
}
grand_margin();//1번문제
});
});
});
하단 스크립트도 금액부분과 마진 검색
$(function () {
$('.subtot1, .grdtot1').prop('readonly', true);
var $tblrows = $("#tblProducts tbody tr");
$tblrows.each(function (index) {
var $tblrow = $(this);
$tblrow.find('.qty1, .price1').on('change', function () {
var qty1 = $tblrow.find("[name=qty1]").val();
var price1 = $tblrow.find("[name=price1]").val();
var subTotal1 = parseInt(qty1, 10) * parseFloat(price1);
if (!isNaN(subTotal1)) {//2번문제
$tblrow.find('.subtot1').val(subTotal1.toFixed(0));
}
else{
$tblrow.find('.subtot1').val(0);
}
var grandTotal1 = 0;
$(".subtot1").each(function () {
var stval = parseFloat($(this).val());
grandTotal1 += isNaN(stval) ? 0 : stval;
});
$('.grdtot1').val(number_format(grandTotal1.toFixed(0)));
}
grand_margin();//1번문제
});
});
});
답변을 작성하시기 전에 로그인 해주세요.