숫자를 더하는 소스를 구하려는데 NaN이 뜨고 있습니다.
본문
<table>
<tr>
<td>1111
<select onChange="sumAll()" id=wr_body_6 class='ed' required name='wr_body_6[]' itemname='1111'>
<option value=''>선택하세요</option>
<option value='69000' <?=$wr_body_6[$i] == '69000' ? 'selected' : ''?>>69000</option>
<option value='74000' <?=$wr_body_6[$i] == '74000' ? 'selected' : ''?>>74000</option>
<option value='69000' <?=$wr_body_6[$i] == '69000' ? 'selected' : ''?>>69000</option>
</select>
</td>
</tr>
<tr>
<td>2222
<select onChange="sumAll()" id=wr_body_6 class='ed' required name='wr_body_6[]' itemname='2222'>
<option value=''>선택하세요</option>
<option value='79000' <?=$wr_body_6[$i] == '79000' ? 'selected' : ''?>>79000</option>
<option value='89000' <?=$wr_body_6[$i] == '89000' ? 'selected' : ''?>>89000</option>
<option value='89000' <?=$wr_body_6[$i] == '89000' ? 'selected' : ''?>>89000</option>
</select>
</td>
</tr>
<tr>
<td>3333
<select onChange="sumAll()" id=wr_body_6 class='ed' required name='wr_body_6[]' itemname='3333'>
<option value=''>선택하세요</option>
<option value='109000' <?=$wr_body_6[$i] == '109000' ? 'selected' : ''?>>109000</option>
<option value='119000' <?=$wr_body_6[$i] == '119000' ? 'selected' : ''?>>119000</option>
<option value='119000' <?=$wr_body_6[$i] == '119000' ? 'selected' : ''?>>119000</option>
</select>
</td>
</tr>
<tr>
<td>4444
<select onChange="sumAll()" id=wr_body_6 class='ed' required name='wr_body_6[]' itemname='4444'>
<option value=''>선택하세요</option>
<option value='0' <?=$wr_body_6[$i] == '0' ? 'selected' : ''?>>0</option>
<option value='200000' <?=$wr_body_6[$i] == '200000' ? 'selected' : ''?>>200000</option>
</select>
</td>
</tr>
<tr>
<td>
총 <input type=text id=totalInput name=totalInput value='0'>
</td>
</tr>
</table>
숫자를 선택하면 하단에 해당 숫자들이 더한 합을 구하려고 합니다.
그런데 합계 부분에 NaN이 뜨고 있습니다.
어떤 부분을 수정해야 숫자로 합계가 뜰까요?
고수님들의 도움을 구합니다.
답변 3
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function sumAll() {
var totalInput = 0;
$('select[name="wr_body_6[]"]').each(function (i, el) {
var num = $(this).val();
totalInput += Number(num);
});
$('#totalInput').val(totalInput);
}
$(function () {
$('select[name="wr_body_6[]"]').each(function (i, el) {
$(el).on('change', sumAll);
});
});
</script>
<table>
<tr>
<td>1111
<select class='ed' required name='wr_body_6[]' itemname='1111'>
<option value=''>선택하세요</option>
<option value='69000'>69000</option>
<option value='74000'>74000</option>
<option value='69000'>69000</option>
</select>
</td>
</tr>
<tr>
<td>2222
<select class='ed' required name='wr_body_6[]' itemname='2222'>
<option value=''>선택하세요</option>
<option value='79000'>79000</option>
<option value='89000'>89000</option>
<option value='89000'>89000</option>
</select>
</td>
</tr>
<tr>
<td>3333
<select class='ed' required name='wr_body_6[]' itemname='3333'>
<option value=''>선택하세요</option>
<option value='109000'>109000</option>
<option value='119000'>119000</option>
<option value='119000'>119000</option>
</select>
</td>
</tr>
<tr>
<td>4444
<select class='ed' required name='wr_body_6[]' itemname='4444'>
<option value=''>선택하세요</option>
<option value='0'>0</option>
<option value='200000'>200000</option>
</select>
</td>
</tr>
<tr>
<td>
총 <input type="text" id="totalInput" name="totalInput" value='0'>
</td>
</tr>
</table>
해당만 가지고는 알 수 없습니다. sumAll 함수에서 처리하시는거 같은데 해당 함수를 살펴보셔야할거 같습니다.
값을 잘 가져오는지 콘솔 찍어보시고
잘가져온다면 보통 숫자형이 아니어서 오류가나는 경우가많습니다.