인풋 숫자? 합계? 하는방법 문의드립니다.
본문
계속 문의만 드려서 죄송합니다...
<?php echo number_format($ct_price['stotal']); ?>
이부분의 숫자를 합계를 하고싶은데 이쪽저쪽 다찾아봐도 초보인 저에게는 너무어려워서요 ㅠㅠ
혹시 예제나 방법 아시는 고수님들계실까요 ?
부탁좀드리겠습니다..
<!-- 반복 -->
<tr>
<td class="td_num">
<label for="ct_qty_<?php echo $chk_cnt; ?>" class="sound_only" value="<?php echo $opt['ct_qty']; ?>"> 수량</label>
<span class="quantity"><?php echo $opt['ct_qty']; ?></span>
</td>
<td class="td_num_right "><input type="text" name="opt_price" class="price" value="<?php echo $opt_price ?>"> </td>
<td class="td_num_right"><span class="calc_price"><?php echo number_format($ct_price['stotal']); ?></span></td>
</tr>
<!-- /반복 -->
<script>
// 반복되는 부분 말고, 한번만 출력되면 OKAY
$('.price').on('keyup', function() {
var quantity = parseInt($(this).closest('tr').find('.quantity').text().replace(/[^\d]+/g, '')) | 0;
var price = parseInt($(this).val().replace(/[^\d]+/g, '')) | 0;
$(this).closest('tr').find('.calc_price').text(quantity*price);
});
</script>
답변 1
5번 반복된다고 가상하고 작성해 보자면
<!-- 반복 -->
<?php
$total = "";
for ($i=0; $i < 5; $i++) { ?>
<tr>
<td class="td_num">
<label for="ct_qty_<?php echo $chk_cnt; ?>" class="sound_only" value="<?php echo $opt['ct_qty']; ?>"> 수량</label>
<span class="quantity"><?php echo $opt['ct_qty']; ?></span>
</td>
<td class="td_num_right "><input type="text" name="opt_price" class="price" value="<?php echo $opt_price ?>"> </td>
<td class="td_num_right"><span class="calc_price"><?php echo number_format($ct_price['stotal']); ?></span></td>
</tr>
<?php $total += $ct_price['stotal'];?>
<?php } ?>
<!-- /반복 -->