2014-02-12 37 views
1
<tr> 
    <td>Knowledge</td> 
    <td><input type="text" name="Knowledge" style="height: 30px; width: 220px;" class="computethis" id="knowledge" Placeholder = "Enter Grade" autocomplete ="off" /></td> 
</tr> 
<tr> 
    <td>Understanding</td> 
    <td><input type="text" name="understanding" style="height: 30px; width: 220px;" class="computethis" id="understanding" Placeholder = "Enter Grade" autocomplete ="off" /></td> 
</tr> 
<tr> 
    <td>Process or Skills</td> 
    <td><input type="text" name="process" style="height: 30px; width: 220px;" class="computethis" id="process" Placeholder = "Enter Grade" autocomplete ="off" /></td> 
</tr> 
<tr> 
    <td>Performance or Products</td> 
    <td><input type="text" name="performance" style="height: 30px; width: 220px;" class="computethis" id="products" Placeholder = "Enter Grade" autocomplete ="off" /></td> 
</tr> 
<tr> 
    <td>Grade Total </td> 
    <td><input type="textbox" name="grade" style="height: 30px; width: 220px;" id="totalgrade" Placeholder = "Average Total" autocomplete ="off" readonly="readonly"></td> 
</tr> 
<tr> 
    <td></td> 
</tr> 
<tr>   
    <td colspan='2'><button class="btn btn-info" style="40px" id="save_button" Onclick="insert();"><i class="fa fa-floppy-o"> Save Grade</i></button><span class="alert alert-success" id="loading"></span></td> 
</tr> 

我只是每個文本框都只有25%我想計算每個文本字段中的特定百分比並計算總分數

<script> 
    // computation of average 
    jQuery(".computethis").keyup(function(){ 
     var sum = 0; 
     var avg = 0; 
     jQuery('.computethis').each(function() { 
      sum += Number(jQuery(this).val()); 
     }); 

     var $allx = jQuery(':text.computethis'); 
     var $emptyx = $allx.filter('[value=""]'); 
     var num = $allx.length - $emptyx.length ; 
     if(num > 0) { avg = Number(sum/num); } 
     // jQuery('#prj_usgtot').val(sum); 
     jQuery('#totalgrade').val(avg.toPrecision(2)); 
    }); 
</script> 

我怎樣才能讓每一個textfield具體百分比,它的onkeyup應自動計算平均獲得總成績。

回答

0

對不起,我還不能評論,但你爲什麼不創建一個我得到我++,因爲它通過每個? 在你的html中,它應該只是文本。文本框不是一種類型。 問題在哪裏?

在這裏,我有一些工作,你可能會喜歡。 它還會檢查是否只有數字

$(this).val().match(/^\d+$/) 

http://jsfiddle.net/4Buk5/

+0

我已經不容許在輸入文本的其他字符或特殊字符驗證,這裏是我的公式,希望它會清除掉你,totalgrade =知識(價值* .15)+理解(價值* .25)+產品(價值* .25)+過程(價值* .25)/ 4,謝謝ans,這也有助於我的其他問題並對我的英語語法感到抱歉 – user3156227