我的Javascript代碼不工作的兩個不同的值,問題在哪裏? 我試圖設置代碼在兩種不同的條件下用兩個不同的值進行分割。但我在這個if else語句中。代碼的鏈接在這裏:https://jsfiddle.net/gLb0uok5/Javascript代碼不能爲兩個不同的值工作
function calc()
{
var m1,m2,m3,avg = 0,total = 0, result = "",grade = "";
m1 = parseInt(document.form1.wp.value);
m2 = parseInt(document.form1.sp.value);
m3 = parseInt(document.form1.cg.value);
total = m1+m2+m3;
if(m3 = 0)
{
avg = total/2;
}
else(m3 >= 1)
{
avg = total/3;
}
document.form1.result.value = result;
document.form1.grade.value = grade;
document.form1.total.value = total;
document.form1.average.value = avg;
}
<form name = "form1">
<table border = "1">
<tr>
<td> Student Name</td>
<td><input type = "text" /></td>
</tr>
<tr>
<td colspan = "2" align = "center">Subject Marks</td>
</tr>
<tr>
<td>Web Programming</td>
<td><input type = "text" name = "wp" /></td>
</tr>
<tr>
<td>Computer Graphics</td>
<td><input type = "text" name = "cg" /></td>
</tr>
<tr>
<td>System Programming</td>
<td><input type = "text" name = "sp" /></td>
</tr>
<tr>
<td colspan = "2" align = "center"><input type = "button" onclick = "calc()" value = "calculte" /></td>
</tr>
<tr>
<td>Total</td>
<td><input type = "text" name = "total"/></td>
</tr>
<tr>
<td>Average</td>
<td><input type = "text" name = "average" /></td>
</tr>
<tr>
<td>Result</td>
<td><input type = "text" name = "result" /></td>
</tr>
<tr>
<td>Grade</td>
<td><input type = "text" name = "grade"/></td>
</tr>
</table>
</form>
請更整齊地碼 –
你有語法錯誤,解釋你的問題。在添加總量之前,你應該檢查m3是否存在。 – Shilly