我想做一個簡單的測試,通過單選按鈕提問和回答標記,但我卡住了。如何在JavaScript中獲得積極答案的總和?
我有6個單獨的測試全部通過10個問題,你會看到代碼。我通過10個問題做了兩個測試,我的問題是讓他們成爲個人,這樣我就可以得到不同的答案。
我想要的是當某人標記爲「是」時,他/她在測試結束時得到肯定答案或「是」答案。
下面是代碼:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function DisplayPrice(price){
var val1 = 0;
for(i = 0; i < document.form1.price.length; i++){
if(document.form1.price[i].checked == true){
val1 = document.form1.price[i].value;
}
}
var val2 = 0;
for(i = 0; i < document.form2.price2.length; i++){
if(document.form2.price2[i].checked == true){
val2 = document.form2.price2[i].value;
}
}
var val3 = 0;
for(i = 0; i < document.form3.price3.length; i++){
if(document.form3.price3[i].checked == true){
val3 = document.form3.price3[i].value;
}
}
var val4 = 0;
for(i = 0; i < document.form4.price4.length; i++){
if(document.form4.price4[i].checked == true){
val4 = document.form4.price4[i].value;
}
}
var val5 = 0;
for(i = 0; i < document.form5.price5.length; i++){
if(document.form5.price5[i].checked == true){
val5 = document.form5.price5[i].value;
}
}
var val6 = 0;
for(i = 0; i < document.form6.price6.length; i++){
if(document.form6.price6[i].checked == true){
val6 = document.form6.price6[i].value;
}
}
var val7 = 0;
for(i = 0; i < document.form7.price7.length; i++){
if(document.form7.price7[i].checked == true){
val7 = document.form7.price7[i].value;
}
}
var val8 = 0;
for(i = 0; i < document.form8.price8.length; i++){
if(document.form8.price8[i].checked == true){
val8 = document.form8.price8[i].value;
}
}
var val9 = 0;
for(i = 0; i < document.form9.price9.length; i++){
if(document.form9.price9[i].checked == true){
val9 = document.form9.price9[i].value;
}
}
var val10 = 0;
for(i = 0; i < document.form10.price10.length; i++){
if(document.form10.price10[i].checked == true){
val10 = document.form10.price10[i].value;
}
}
var sum=parseInt(val1) + parseInt(val2) + parseInt(val3) + parseInt(val4) + parseInt(val5) + parseInt(val6) + parseInt(val7) + parseInt(val8) + parseInt(val9) + parseInt(val10);
document.getElementById('totalSum').value=sum;
}
</script>
<style type="text/css">
body,td,th {
color: #03C;
}
body {
background-color: #999;
text-align: left;
}
</style>
</head>
<body>
<h2><strong>TEST 1</strong></h2>
<form name="form1" id="form1" runat="server">
<h3><br>
QUESTION1.
<input id="rdo_1" type="radio" value="1" name="price" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form2" id="form2" runat="server">
<h3><br>
QUESTION2.
<input id="rdo_1" type="radio" value="1" name="price2" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price2" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form3" id="form3" runat="server">
<h3><br>
QUESTION3.
<input id="rdo_1" type="radio" value="1" name="price3" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price3" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form4" id="form4" runat="server">
<h3><br>
QUESTION4.
<input id="rdo_1" type="radio" value="1" name="price4" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price4" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form5" id="form5" runat="server">
<h3><br>
QUESTION5.
<input id="rdo_1" type="radio" value="1" name="price5" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price5" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form6" id="form6" runat="server">
<h3><br>
QUESTION6.
<input id="rdo_1" type="radio" value="1" name="price6" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price6" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form7" id="form7" runat="server">
<h3>QUESTION7.
<input id="rdo_1" type="radio" value="1" name="price7" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price7" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form8" id="form8" runat="server">
<h3><br>
QUESTION8.
<input id="rdo_1" type="radio" value="1" name="price8" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price8" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form9" id="form9" runat="server">
<h3><br>
QUESTION9.
<input id="rdo_1" type="radio" value="1" name="price9" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price9" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form10" id="form10" runat="server">
<h3>QUESTION10.
<input id="rdo_1" type="radio" value="1" name="price10" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_2" type="radio" value="0" name="price10" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<p> </p>
<h2><strong>TEST 2</strong></h2>
<form name="form1" id="form11" runat="server">
<h3><br>
QUESTION1.
<input id="rdo_3" type="radio" value="1" name="price" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form2" id="form12" runat="server">
<h3><br>
QUESTION2.
<input id="rdo_3" type="radio" value="1" name="price2" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price2" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form3" id="form13" runat="server">
<h3><br>
QUESTION3.
<input id="rdo_3" type="radio" value="1" name="price3" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price3" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form4" id="form14" runat="server">
<h3><br>
QUESTION4.
<input id="rdo_3" type="radio" value="1" name="price4" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price4" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form5" id="form15" runat="server">
<h3><br>
QUESTION5.
<input id="rdo_3" type="radio" value="1" name="price5" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price5" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form6" id="form16" runat="server">
<h3><br>
QUESTION6.
<input id="rdo_3" type="radio" value="1" name="price6" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price6" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form7" id="form17" runat="server">
<h3>QUESTION7.
<input id="rdo_3" type="radio" value="1" name="price7" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price7" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form8" id="form18" runat="server">
<h3><br>
QUESTION8.
<input id="rdo_3" type="radio" value="1" name="price8" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price8" onClick="DisplayPrice(this.value);">
NO<br>
</h3>
</form>
<form name="form9" id="form19" runat="server">
<h3><br>
QUESTION9.
<input id="rdo_3" type="radio" value="1" name="price9" onClick="DisplayPrice(this.value);">
YES
<input id="rdo_4" type="radio" value="0" name="price9" onClick="DisplayPrice(this.value);">
NO <br>
</h3>
</form>
<form name="form10" id="form20" runat="server">
<h3><br>
QUESTION10.
<input id="rdo_3" type="radio" value="1" name="price10" onClick="DisplayPrice(this.value);">
DA
<input id="rdo_4" type="radio" value="0" name="price10" onClick="DisplayPrice(this.value);">
NE<br>
</h3>
</form>
<h2>Score "TEST1"
<input type="text" name="totalSum" id="totalSum" value="" size="3" readonly="readonly">
</h2>
<h2>Score "TEST2"
<input type="text" name="totalSum2" id="totalSum2" value="" size="3" readonly="readonly">
</h2>
</body>
劉若英,非常感謝你,一切都正常工作;) – user2092760 2013-02-23 11:40:38
卻得到了一個新的問題:/我需要6次個人測試中,我讓4他們和..現在反混合nubers:/我將編輯我的答案,並把新的代碼在其中..如果你可以看看 – user2092760 2013-02-23 12:07:32
你最好接受這個答案,並提出新的問題。假設你保持html格式類似,我已經更新了這個版本以使用4個測試。 – rene 2013-02-23 13:21:39