這裏我計算了一些PO(程序結果)的平均值。在計算平均數後,我必須通過比較一些條件陳述來顯示達到水平或說成績。條件是:如何使用javascript給出一些條件語句時計算某個等級
- 如果平均值爲
>= 80
,則達到的值爲'H'
。 - 如果平均值爲
>= 60
但是< 80
,則達到的值爲'M'
。 - 如果平均值爲
>= 40
但是< 60
,則達到的值爲'L'
。
我已經計算了平均值,但是找不到計算達到水平的邏輯。下面給出了我完成的代碼的一部分。
<script type="text/jscript">
function calcAvg(input_id, output_id) {
//Get all elements with 'class="select"'
var selects = document.getElementsByClassName(input_id);
//Initialize vars
var avg = 0;
var count = 0;
//Calculate average
for (var i = 0; i < selects.length; i++) {
if (selects[i].value != "") {
count++;
avg += Number(selects[i].value);
//Alert for debugging purposes
//alert(selects[i].value+" "+avg);
}
}
avg = avg/count;
//Output average
document.getElementById(output_id).value = avg;
}
</script>
<table>
<tr>
<td height="41" colspan="12" align="center">ATTAINMENT OF PO(PO-CO MAPPING)
</td>
</tr>
<tr>
<td width="17%" rowspan="2" align="center">NAME OF THE MODULES</td>
<td height="34" colspan="11" align="center">PROGRAME OUTCOMES</td>
</tr>
<tr>
<td width="7%" align="center">PO1</td>
<td width="7%" align="center">PO2</td>
</tr>
<tr>
<td height="71" align="center">MATHEMATICS</td>
<td align="center"><input type="number" class="select1" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select1','calculation1');" style="width:60px"> </td>
<td align="center"><input type="number" class="select2" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select2','calculation2');" style="width:60px"> </td>
</tr>
<tr>
<td height="71" align="center">SCIENCE</td>
<td align="center"><input type="number" class="select1" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select1','calculation1');" style="width:60px"> </td>
<td align="center"><input type="number" class="select2" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select2','calculation2');" style="width:60px">
</td>
</tr>
<tr bgcolor="#9999CC">
<td height="71" align="center">PO AVERAGES</td>
<td align="center"><input type="text" name="Avg" id="calculation1" readonly style="width:60px"> </td>
<td align="center"><input type="text" name="Avg1" id="calculation2" readonly style="width:60px"> </td>
</tr>
<tr>
<td height="71" align="center">PO ATTAINMENT</td>
<td align="center"><input type="text" name="Att1" id="calatt1" readonly style="width:60px"> </td>
<td align="center"><input type="text" name="Att2" id="calatt2" readonly style="width:60px"> </td>
</tr>
</table>
是如何將平均計算後立即自動顯示達成水平?
謝謝@RobG我已經做了修改 – Omi