2016-11-29 136 views
0

我得到了下面的代碼:與JavaScript有問題的計算的onclick

<form action ="" name="References" > 
    <table> 
    <tr> 
     <td> 
     Reference 1 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference1"> 
       <option value="4">Excellent</option> 
       <option value="3">Good</option> 
       <option value="2">Neutral</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 2 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference2"> 
       <option value="4">Excellent</option> 
       <option value="3">Good</option> 
       <option value="2">Neutral</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 3 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference3"> 
       <option value="4">Excellent</option> 
       <option value="3">Good</option> 
       <option value="2">Neutral</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Average rating 
     </td> 
     <td> <input type="text" name="averageRating" size="4" />   
     </td> 
    </tr> 
    </table> 
    <p> 
    <input type="button" value="Calculate" onclick=" 

     document.forms['References'].elements['averageRating'].value = 0; 
     for (x = 1 ; x <=3; x++) { 
      document.forms['References'].elements['averageRating'].value = 
       parseInt(document.forms['References'].elements['averageRating'].value) + 
              parseInt(document.forms['References'].elements['reference'+x].value);  
     } 
      document.forms['References'].elements['averageRating'].value = 
       parseInt(document.forms['References'].elements['averageRating'].value)/3; 
     "; /> 
    </p> 
    </form> 

我只好再編輯代碼,包括6名,我因此標有「參考」,但我的onclick似乎並沒有被對。除了增加3個引用到現有的3個(總共6個)之外,我還必須爲每個引用1個值。我所擁有的是這樣的:

<form action ="" name="References" > 
    <table> 
    <tr> 
     <td> 
     Reference 1 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference1"> 
       <option value="5">Very Satisfied</option> 
       <option value="4">Satisfied</option> 
       <option value="3">Neutral</option> 
       <option value="2">Dissatisfied</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 2 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference2"> 
       <option value="5">Very Satisfied</option> 
       <option value="4">Satisfied</option> 
       <option value="3">Neutral</option> 
       <option value="2">Dissatisfied</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 3 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference3"> 
       <option value="5">Very Satisfied</option> 
       <option value="4">Satisfied</option> 
       <option value="3">Neutral</option> 
       <option value="2">Dissatisfied</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 4 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference4"> 
       <option value="5">Very Satisfied</option> 
       <option value="4">Satisfied</option> 
       <option value="3">Neutral</option> 
       <option value="2">Dissatisfied</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 5 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference5"> 
       <option value="5">Very Satisfied</option> 
       <option value="4">Satisfied</option> 
       <option value="3">Neutral</option> 
       <option value="2">Dissatisfied</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Reference 6 
     </td> 
     <td> 
      <a href=""></a> 
      <select name="reference6"> 
       <option value="5">Very Satisfied</option> 
       <option value="4">Satisfied</option> 
       <option value="3">Neutral</option> 
       <option value="2">Dissatisfied</option> 
       <option value="1">I will not recommend it</option> 
      </select>    
     </td> 
    </tr> 
    <tr> 
     <td> 
     Average rating 
     </td> 
     <td> <input type="text" name="averageRating" size="4" />   
     </td> 
    </tr> 
    </table> 
    <p> 
    <input type="button" value="Calculate" onclick=" 

     document.forms['References'].elements['averageRating'].value = 0; 
     for (x = 1 ; x <=4; x++) { 
      document.forms['References'].elements['averageRating'].value = 
       parseInt(document.forms['References'].elements['averageRating'].value) + 
              parseInt(document.forms['References'].elements['reference'+x].value);  
     }    
      document.forms['References'].elements['averageRating'].value = 
       parseInt(document.forms['References'].elements['averageRating'].value)/6; 
     "; /> 

6的得分爲5各等級應爲30,然後除以6(獲取平均)我應該結束了5,這是我做的。但是如果我有一個5分的評分,那麼剩下的1分,即5 + 1 + 1 + 1 + 1 + 1 = 10和10/6是1.67,但最終我的答案是1.33。這個補充似乎沒有適當地加起來,我不確定還有其他什麼,或者我應該編輯哪些內容才能正確使用。

回答

0

你只有六個要素四迭代中,對於條件更改爲x <= 6

+0

哇,這似乎也起了重要作用。我已經將它改爲x <= 4,認爲它意味着選項值2-4,其中x = 1; X <= 4; X ++。非常感謝! Javascript很有趣,但對我來說很陌生。 –