有javascript的问题计算onclick

时间:2016-11-29 01:23:11

标签: javascript

我收到了以下代码:

    <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; 
       "; />
每个5分的6个等级应该是30,然后除以6(得到平均值)我应该得到5,我这样做。但是如果我在5的评级是1,那么其余的在1,即5 + 1 + 1 + 1 + 1 + 1 = 10和10/6是1.67,但我的答案是1.33。添加似乎没有正确添加,我不确定在哪里,或者我应该编辑什么来实现这一点。

1 个答案:

答案 0 :(得分:0)

您只迭代六个元素中的四个,将for条件更改为x <= 6