질문의 총합계를 구하고 싶은데 계속 개당 질문에 답변이 구해집니다 채택완료
가가양
10년 전
조회 2,555
안녕하세요
라디오 버튼 합계를 구하는건데
0 , 1, 2, 3, 4
중에 한가지만 선택했을때의 합계를 구하고 싶습니다
지금 되어있는건 질문1의 4를 선택했을때와 다시 3을 선택했을때 합이 구해져서요
질문에 대한 답변을 선택하였을때 총합계를 구하고 싶어요
질문1 답변의 01234 중 한개의 값
질문2 답변의 01234 중 한개의 값
질문3 답변의 01234 중 한개의 값
....
총 질문에 대한 답변의 값
도와주세요
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title> New Document </title> </head> <body><script type="text/javascript"> <!-- function select_item(obj) { with(obj.form) { if(obj.checked == false) { hidd_total.value = eval(hidd_total.value) - eval(hidd_radio.value); hidd_radio.value = eval(obj.value); } else { hidd_radio.value = eval(obj.value); hidd_total.value = eval(hidd_total.value) + eval(obj.value); } return(select_sum(hidd_total.value)); } } function select_sum(no) { no = no.toString().replace(/\$|\,/g,''); if(isNaN(no)) no = "0"; cents = Math.floor((no*100+0.5)%100); no = Math.floor((no*100+0.5)/100).toString(); if(cents < 10) cents = "0" + cents; for (var i = 0; i < Math.floor((no.length-(1+i))/3); i++) no = no.substring(0,no.length-(4*i+3))+','+no.substring(no.length-(4*i+3)); return (no); } function veiw_total() { var frm = document.questionform; frm.total.value = "0"; frm.hidd_total.value = 0; frm.hidd_radio.value = 0; for (var n=0; n < frm.elements.length; n++) { if (frm.elements[n].type == 'checkbox' | frm.elements[n].type == 'radio') { frm.elements[n].checked = false; } } } window.onload = function() { veiw_total(); } //--></script> <div>질문에 대한 합계를 구합니다 ;;</div><form method="post" name="questionform" > <p>질문1.</p><input type="radio" name="question1" value="0" onclick="this.form.total.value=select_item(this);" />1) ㄱㄱㄱ <input type="radio" name="question1" value="1" onclick="this.form.total.value=select_item(this);" />2) ㄴㄴㄴ<input type="radio" name="question1" value="2" onclick="this.form.total.value=select_item(this);" />3) ㄷㄷㄷ<input type="radio" name="question1" value="3" onclick="this.form.total.value=select_item(this);" />4) ㄹㄹㄹ<input type="radio" name="question1" value="4" onclick="this.form.total.value=select_item(this);" />5) ㅁㅁㅁ <p>질문2.</p><input type="radio" name="question2" value="0" onclick="this.form.total.value=select_item(this);" />1) ㄱㄱㄱ<input type="radio" name="question2" value="1" onclick="this.form.total.value=select_item(this);" />2) ㄴㄴㄴ<input type="radio" name="question2" value="2" onclick="this.form.total.value=select_item(this);" />3) ㄷㄷㄷ<input type="radio" name="question2" value="3" onclick="this.form.total.value=select_item(this);" />4) ㄹㄹㄹ<input type="radio" name="question2" value="4" onclick="this.form.total.value=select_item(this);" />5) ㅁㅁㅁ<div> <input type="hidden" name="hidd_total" value="0" /> <input type="hidden" name="hidd_radio" value="0" /> <input type="text" name="total" id="total" value="" readonly style="text-align:center;background-color:red"/></div></form> </body></html>
댓글을 작성하려면 로그인이 필요합니다.
답변 2개
채택된 답변
+20 포인트
10년 전
</p><p><meta charset="utf-8" />
</script>
<input type="radio" name="question1" value="0" onclick="_total()" />1) ㄱㄱㄱ
<input type="radio" name="question1" value="1" onclick="_total()" />2) ㄴㄴㄴ
<input type="radio" name="question1" value="2" onclick="_total()" />3) ㄷㄷㄷ
<input type="radio" name="question1" value="3" onclick="_total()" />4) ㄹㄹㄹ
<input type="radio" name="question1" value="4" onclick="_total()" />5) ㅁㅁㅁ
</p><p><p>질문2.</p>
<input type="radio" name="question2" value="0" onclick="_total()" />1) ㄱㄱㄱ
<input type="radio" name="question2" value="1" onclick="_total()" />2) ㄴㄴㄴ
<input type="radio" name="question2" value="2" onclick="_total()" />3) ㄷㄷㄷ
<input type="radio" name="question2" value="3" onclick="_total()" />4) ㄹㄹㄹ
<input type="radio" name="question2" value="4" onclick="_total()" />5) ㅁㅁㅁ</p><p><input type="text" name="total" id="total" value="" readonly style="text-align:center;background-color:red"/>
<script type="text/javascript">
function _total() {
var radio = document.querySelectorAll("input:checked");
var total = i = 0;
for(i=0; i<radio.length; i++) total += radio[i].value * 1;
document.getElementById("total").value = total;
}
</script></p><p>
참고로 querySelectorAll() 메소드는 ie 같은 경우 9 이상에서만 작동합니다.
그리고 jquery 사용하시는 게 훨씬 편하실 텐데...
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인