계산을 여러개를 해야 하는데...에러가 나네요..도와 주세요.
본문
<script>
$(function() {
$('#chk').on('click', function() {
var wr_7 = parseFloat($('#wr_7').val());
var wr_14 = parseInt($('#wr_14').val());
var wr_15 = parseInt($('#wr_15').val());
tmp = ((wr_14 * 0.007) + wr_15) / wr_7;
val = tmp.toFixed(2);
$('#wr_16').val(val);
});
});
</script>
<caption>[ 임 료 ]</caption>
<tr>
<th scope="row"><label for="wr_7">전용면적</label></th>
<td><input type="text" name="wr_7" value="<?php echo $write['wr_7'] ?>" id="wr_7" class="frm_input" size="10"></td>
</tr>
<tr>
<th scope="row"><label for="wr_14">보증금</label></th>
<td><input type="text" name="wr_14" value="<?php echo $write['wr_14'] ?>" id="wr_14" class="frm_input" size="10"></td>
</tr>
<tr>
<th scope="row"><label for="wr_15">월세</label></th>
<td><input type="text" name="wr_15" value="<?php echo $write['wr_15'] ?>" id="wr_15" class="frm_input" size="10"></td>
</tr>
<caption>=</caption>
<input type="checkbox" id="chk">
<tr>
<th scope="row"><label for="wr_16">임료 계산값</label></th>
<td><input type="text" name="wr_16" value=" <?php echo $write['wr_16']?>" id="wr_16" class="frm_input" size="10"></td>
</tr>
<script>
$(function() {
$('#chk1').on('click', function() {
var wr_13 = parseFloat($('#wr_13').val());
var wr_8 = parseInt($('#wr_8').val());
tmp1 = (wr_13 / wr_8);
val = tmp1.toFixed(2);
$('#wr_9').val(val);
});
});
</script>
<p></p>
<caption>[평당가격]</caption>
<tr>
<th scope="row"><label for="wr_13">매매가격</label></th>
<td><input type="text" name="wr_13" value="<?php echo $write['wr_13'] ?>" id="wr_13" class="frm_input" size="10"></td>
</tr>
<tr>
<th scope="row"><label for="wr_8">분양면적</label></th>
<td><input type="text" name="wr_8" value="<?php echo $write['wr_8'] ?>" id="wr_8" class="frm_input" size="10"></td>
</tr>
<caption>=</caption>
<input type="checkbox" id="chk">
<tr>
<th scope="row"><label for="wr_9">평당가격 계산값</label></th>
<td><input type="text" name="wr_9" value=" <?php echo $write['wr_9']?>" id="wr_9" class="frm_input" size="10"></td>
</tr>
<script>
$(function() {
$('#chk2').on('click', function() {
var wr_10 = parseFloat($('#wr_10').val());
var wr_12 = parseInt($('#wr_12').val());
tmp2 = (wr_10 + wr_12);
val = tmp2.toFixed(2);
$('#wr_13').val(val);
});
});
</script>
<p></p>
<caption>[매매가격]</caption>
<tr>
<th scope="row"><label for="wr_10">분양가격</label></th>
<td><input type="text" name="wr_10" value="<?php echo $write['wr_10'] ?>" id="wr_10" class="frm_input" size="10"></td>
</tr>
<tr>
<th scope="row"><label for="wr_12">프리미엄</label></th>
<td><input type="text" name="wr_12" value="<?php echo $write['wr_12'] ?>" id="wr_12" class="frm_input" size="10"></td>
</tr>
<caption>=</caption>
<input type="checkbox" id="chk">
<tr>
<th scope="row"><label for="wr_13">매매가격 계산값</label></th>
<td><input type="text" name="wr_13" value=" <?php echo $write['wr_13']?>" id="wr_13" class="frm_input" size="10"></td>
</tr>
<script>
$(function() {
$('#chk3').on('click', function() {
var wr_13 = parseFloat($('#wr_13').val());
var wr_14 = parseInt($('#wr_14').val());
var wr_15 = parseInt($('#wr_15').val());
tmp3 = ((wr_15 * 12) / (wr_13 - wr_14);
val = tmp3.toFixed(2);
$('#wr_17').val(val);
});
});
</script>
<p></p>
<caption>[ 수익율 ]</caption>
<tr>
<th scope="row"><label for="wr_13">매매가격</label></th>
<td><input type="text" name="wr_13" value="<?php echo $write['wr_13'] ?>" id="wr_13" class="frm_input" size="10"></td>
</tr>
<tr>
<th scope="row"><label for="wr_14">보증금</label></th>
<td><input type="text" name="wr_14" value="<?php echo $write['wr_14'] ?>" id="wr_14" class="frm_input" size="10"></td>
</tr>
<tr>
<th scope="row"><label for="wr_15">월세</label></th>
<td><input type="text" name="wr_15" value="<?php echo $write['wr_15'] ?>" id="wr_15" class="frm_input" size="10"></td>
</tr>
<caption>=</caption>
<input type="checkbox" id="chk">
<tr>
<th scope="row"><label for="wr_17">수익율 계산값</label></th>
<td><input type="text" name="wr_17" value=" <?php echo $write['wr_17']?>" id="wr_17" class="frm_input" size="10"></td>
</tr>
답변 1
Javascript로 계산식에서 에러가 나는경우는
1. 할당되지 않은 값을 계산하려고 하거나
2. 0을 나누려고 하거나
3. null을 손대거나 하면
계산오류가 발생합니다.
형변환을 하는 시점에도 마찬가지 입니다.
이때는 값이 존재하는지 먼저 확인이 필요합니다.
위에 다양하게 존재해서
예를 들어드리면
var wr_7 = parseFloat($('#wr_7').val());
인경우
wr_7의 값이 아무것도 없는시점에 이벤트가 발생하면 오류가 납니다.
이걸 오류가 없게 변경하려면
var wr_7 = $('#wr_7').val();
if (wr_7 && wr_7 > 0)
wr_7 = parseFloat(wr_7);
와 같이 값이 parseFloat가 작동할때 오류가 없는 충분한 조건을 만들어주는 방법입니다.
다른 경우의 계산식도 마찬가지 입니다.
값을 나눌때도 앞서 체크흘 해야죠