2017-12-03 11 views
0

我正在製作一個網站頁面,這是一個調查的種類。我有一節主要工作,但我不能得到下半場。我使用了與第一部分相同的代碼並進行了擴展。儘管如此,我還是無法使用它。我不明白爲什麼當我點擊提交時不會寫分數。當我按下提交時,它應該寫下「這是答案」,並且應該從問題的值中計算點的數量,例如,如果他們按下每個問題的第一個單選按鈕,它應該打印出2. 2.通過提交得分

這是我使用的基本JavaScript,以防它有所幫助。

function answer(total) { 
var score = 0; 
if (document.getElementById('exp_no').checked) { 
score++; 
} 
if (document.getElementById('chg_no').checked) { 
score++; 
} 
if (document.getElementById('sus_no').checked) { 
score++; 
} 
document.getElementById('totalScore').innerHTML = score; 

} 

這是我正在使用的JavaScript。

function answer2(total) { 
var score2 = 0; 
if (document.getElementById('arr_1').checked) { 
score2++; 
} 
else if (document.getElementById('arr_2').checked) { 
score2 + 2; 
} 
else if (document.getElementById('arr_3').checked) { 
score2 + 3; 
} 
else if (document.getElementById('arr_4').checked) { 
score2 + 4; 
} 
else (document.getElementById('arr_5').checked) { 
score2 + 5; 
} 

if (document.getElementById('been1').checked) { 
score2++; 
} 
else if (document.getElementById('been2').checked) { 
score2 + 2; 
} 
else if (document.getElementById('been3').checked) { 
score2 + 3; 
} 
else if (document.getElementById('been4').checked) { 
score2 + 4; 
} 
else if (document.getElementById('been5').checked) { 
score2 + 5; 
} 
if (score2 == 2) { 
document.getElementById('finalScore').innerHTML = score2; 
} else if (score2 == 4){ 
document.getElementById('finalScore').innerHTML = score2; 
} else if (score2 == 4){ 
document.getElementById('finalScore').innerHTML = score2; 
} else if (score2 == 6){ 
document.getElementById('finalScore').innerHTML = score2; 
} else if (score2 == 8){ 
document.getElementById('finalScore').innerHTML = score2; 
} else if (score2 == 10){ 
document.getElementById('finalScore').innerHTML = score2; 
} 
} 

這是我的HTML

<button onclick = "toggletab()" id="tabButton"><h3>first results</h3> 
</button> 
     <form> 
     <div id="first" > 
     <fieldset> 
       <label> 
       <legend>Is your arrest record a:</legend> 
        <input id="arr_1" type="radio" name="field4" value="1" 
onclick="getscores4(this)"/> 
        IC 35-38-9-1 
       </label> 
       <label> 
        <input id="arr_2" type="radio" name="field4" value="2" 
onclick="getscores4(this)"/> 
        IC 35-38-9-2 
       </label> 
       <label> 
        <input id="arr_3" type="radio" name="field4" value="3" 
onclick="getscores4(this)"/> 
        IC 35-38-9-3 
       </label> 
       <label> 
        <input id="arr_4" type="radio" name="field4" value="4" 
onclick="getscores4(this)"/> 
        IC 35-38-9-4 
       </label> 
       <label> 
        <input id="arr_5" type="radio" name="field4" value="5" 
onclick="getscores4(this)"/> 
        IC 35-38-9-5 
       </label> 
      </fieldset> 


      <fieldset> 
      <label> 
       <legend>Has it been:</legend> 
        <input id="been1" type="radio" name="field5" value="1" 
onclick="getscores5(this)"/> 
       1 Year From Date of Arrestor earlier if the Prosecutor 
agrees 
       </label> 
       <label> 
        <input id="been2" type="radio" name="field5" value="2" 
onclick="getscores5(this)"/> 
        5 Years From Date of Arrestor earlier if the Prosecutor 
agrees 
       </label> 
       <label> 
        <input id="been3" type="radio" name="field5" value="3" 
onclick="getscores5(this)"/> 
        8 YearsFrom Date of Arrestor earlier if the Prosecutor 
agrees 
       </label> 
       <label> 
        <input id="been4" type="radio" name="field5" value="4" 
onclick="getscores5(this)"/> 
        8/3 Years The Later of 8 Years from Date of Conviction 
or 3 years from completion of the sentence or earlier if the Prosecutor 
agrees 
       </label> 
       <label> 
        <input id="been5" type="radio" name="field5" value="5" 
onclick="getscores5(this)"/> 
        10/5 Years The Later of 10 Years from Date of 
Conviction or 5 years from completion of the sentence or earlier if the 
Prosecutor agrees 
       </label> 
      </fieldset> 
     </div> 
     <fieldset id="submitbutton" class="article"> 
<input type="button" id="submit" value="submit" onclick='answer2()' /> 
<p id="finalScore">this is answer </p> 
</fieldset> 
     </form> 
     </div> 
     </div> 

     <script src="backtest.js"></script> 
     <script src="backtest2.js"></script> 
     <script src="toggle.js"></script> 
+0

這段代碼有很多問題。你究竟想幹什麼? – brk

+0

我需要添加問題的值。我打算有5個不同的結果,取決於最終值是否等於2,4,6,8或10.這應該顯示結果#取決於最終值等於多少個數。因此,如果選擇了arr_1和been1,最終值應爲2並顯示結果1。 –

回答

0

在功能ANSWER2(總數)已宣佈名爲「score2」變量,但在所有你的邏輯,你正在使用名爲「分數」變量。將分數更改爲分數2,然後它將起作用。而且我也認爲answer2()函數中不需要「全部」參數,因爲您沒有在任何地方使用它。