使用javascript创建页面以创建模块的子总标记到整个模块标记: 我想创建一个函数来计算像这样的小计
小计= Log1 * 10/24 + Log2 * 10/24 + ClassTest * 10/50。
我希望它显示小计的值,如果小计小于12,则显示:“圣诞快乐,下学期要努力工作!”否则,显示:“圣诞快乐,继续保持!”但是我&#39 ;努力让代码工作,并想知道为什么会这样。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" language="JavaScript">
function doSomething(num1,num2,num3,){
var numb1 = parseInt(document.myform.num1.value);
var numb2 = parseInt(document.myform.num2.value);
var numb3 = parseInt(document.myform.num3.value);
if (isNaN(numb1)) {
window.alert("Mark 1 is not a valid number!");
}
else if (isNaN(numb2)) {
window.alert("Mark 2 is not a valid number!");
}
else if (isNaN(numb3)) {
window.alert("Mark 3 is not a valid number!");
}
else if (numb1 > 100 || numb2 > 100 || numb3 > 100 ) {
window.alert("Marks must not be higher than 100!");
return;
}
else if (numb1 < 0 || numb2 < 0 || numb3 < 0 ) {
window.alert("Marks must not be negative!");
return;
}
//if…else if…else statement
log1=numb1*10/24;
log2=numb2*10/24;
test=numb3*10/50;
subtotal = log1+log2+test;
if (subtotal > 12)
{
window.alert( subtotal +"Merry Christmas and Work harder next semester!");
}
else if (avg <= 12)
{
window.alert(subtotal +"Merry Christmas and Keep it up!”);
}
</script>
</head>
<body bgcolor="white">
<p>Please input your marks and click the button to start:</p>
<form name="myform">
<p> Log 1 Mark :</p>
<input type="text" size="10" name="num1">
<p> Log 2 Mark:</p>
<input type="text" size="10" name="num2">
<p> Class Test Mark:</p>
<input type="text" size="10" name="num3">
<br>
<input type="button" name="mybutton" value="Continue" onclick="doSomething(num1,num2,num3);">
</form>
</body>
</html>
答案 0 :(得分:0)
else if (avg <= 12)
您正在测试一个您从未在任何地方定义过的变量。这可能是你的问题吗?
答案 1 :(得分:0)
您的代码存在许多编译问题
subtotal
和average
,而average
未定义document.myform.num1.value
按预期工作作为提示,只需打开Developer Tools即可发现这些错误
function doSomething() {
var numb1 = parseInt(document.myform.num1.value);
var numb2 = parseInt(document.myform.num2.value);
var numb3 = parseInt(document.myform.num3.value);
if (isNaN(numb1)) {
window.alert("Mark 1 is not a valid number!");
return;
}
else if (isNaN(numb2)) {
window.alert("Mark 2 is not a valid number!");
return;
}
else if (isNaN(numb3)) {
window.alert("Mark 3 is not a valid number!");
return;
}
else if (numb1 > 100 || numb2 > 100 || numb3 > 100) {
window.alert("Marks must not be higher than 100!");
return;
}
else if (numb1 < 0 || numb2 < 0 || numb3 < 0) {
window.alert("Marks must not be negative!");
return;
}
//if…else if…else statement
var log1 = numb1 * 10 / 24;
var log2 = numb2 * 10 / 24;
var test = numb3 * 10 / 50;
var subtotal = log1 + log2 + test;
if (subtotal > 12) {
window.alert(subtotal + "Merry Christmas and Work harder next semester!");
}
else if (subtotal <= 12) {
window.alert(subtotal + "Merry Christmas and Keep it up!”);
}
}