[JS] 문항 합계 구하기 - 배열
<div>
<table id="question">
<tr>
<th>1번 문항</th>
<td><input type="radio" name="question1" id="qone1" value="1" /></td>
<td><input type="radio" name="question1" id="qone2" value="2" /></td>
<td><input type="radio" name="question1" id="qone3" value="3" /></td>
<td><input type="radio" name="question1" id="qone4" value="4" /></td>
</tr>
<tr>
<th>2번 문항</th>
<td><input type="radio" name="question2" id="qtwo1" value="1" /></td>
<td><input type="radio" name="question2" id="qtwo2" value="2" /></td>
<td><input type="radio" name="question2" id="qtwo3" value="3" /></td>
<td><input type="radio" name="question2" id="qtwo4" value="4" /></td>
</tr>
<tr>
<th>3번 문항</th>
<td><input type="radio" name="question3" id="qthr1" value="1" /></td>
<td><input type="radio" name="question3" id="qthr2" value="2" /></td>
<td><input type="radio" name="question3" id="qthr3" value="3" /></td>
<td><input type="radio" name="question3" id="qthr4" value="4" /></td>
</tr>
<tr>
<th>4번 문항</th>
<td><input type="radio" name="question4" id="qfou1" value="1" /></td>
<td><input type="radio" name="question4" id="qfou2" value="2" /></td>
<td><input type="radio" name="question4" id="qfou3" value="3" /></td>
<td><input type="radio" name="question4" id="qfou4" value="4" /></td>
</tr>
</table>
</div>
<p class="result" id="result"></p>
<script type="text/javascript">
$(document).ready(function(){
var qRadio = $('#question').find('input[type=radio]');
totalArry = [];
qRadio.on('click', function(){
var $this = $(this),
$thisName = $this.attr('name'),
$thisOrd = ($thisName.split('question')[1]) -1,
$thisVal = $this.attr('value'),
total = 0;
console.log(totalArry);
totalArry[$thisOrd] = $thisVal;
for (var i = 0; i < totalArry.length; i++){
total += parseInt(totalArry[i]);
}
qResult(total);
});
});
function qResult(total){
var result = $('#result');
result.text(total);
}
</script>
<table id="question">
<tr>
<th>1번 문항</th>
<td><input type="radio" name="question1" id="qone1" value="1" /></td>
<td><input type="radio" name="question1" id="qone2" value="2" /></td>
<td><input type="radio" name="question1" id="qone3" value="3" /></td>
<td><input type="radio" name="question1" id="qone4" value="4" /></td>
</tr>
<tr>
<th>2번 문항</th>
<td><input type="radio" name="question2" id="qtwo1" value="1" /></td>
<td><input type="radio" name="question2" id="qtwo2" value="2" /></td>
<td><input type="radio" name="question2" id="qtwo3" value="3" /></td>
<td><input type="radio" name="question2" id="qtwo4" value="4" /></td>
</tr>
<tr>
<th>3번 문항</th>
<td><input type="radio" name="question3" id="qthr1" value="1" /></td>
<td><input type="radio" name="question3" id="qthr2" value="2" /></td>
<td><input type="radio" name="question3" id="qthr3" value="3" /></td>
<td><input type="radio" name="question3" id="qthr4" value="4" /></td>
</tr>
<tr>
<th>4번 문항</th>
<td><input type="radio" name="question4" id="qfou1" value="1" /></td>
<td><input type="radio" name="question4" id="qfou2" value="2" /></td>
<td><input type="radio" name="question4" id="qfou3" value="3" /></td>
<td><input type="radio" name="question4" id="qfou4" value="4" /></td>
</tr>
</table>
</div>
<p class="result" id="result"></p>
<script type="text/javascript">
$(document).ready(function(){
var qRadio = $('#question').find('input[type=radio]');
totalArry = [];
qRadio.on('click', function(){
var $this = $(this),
$thisName = $this.attr('name'),
$thisOrd = ($thisName.split('question')[1]) -1,
$thisVal = $this.attr('value'),
total = 0;
console.log(totalArry);
totalArry[$thisOrd] = $thisVal;
for (var i = 0; i < totalArry.length; i++){
total += parseInt(totalArry[i]);
}
qResult(total);
});
});
function qResult(total){
var result = $('#result');
result.text(total);
}
</script>
댓글
댓글 쓰기