如果输入数字标记,我有一个switch语句给你一个字母标记。我没有从chrome得到任何错误,所以我不知道我做错了什么。我只是格式化它错了吗?
<html>
<body>
<h1>Rank Your Mark!!</h1>
Enter your mark to rank it.
<input type="number" name="grade" id="mark">
<input type="button" name="mark" id="grade" value="Rate" onclick="rateMark()">
<script>
function rateMark(){
switch(grade)
{
case"F":
mark > 50;
break;
case"D":
mark >= 50 && mark <=59.9;
break;
case"C":
mark >= 60 && mark <=69.9;
break;
case"B":
mark >= 70 && mark <= 79.9;
break;
case"A":
mark >= 80 && mark <= 89.9;
break;
case"A+":
mark >= 90 && mark <= 100;
break;
}
document.getElementById("demo").innerHTML = "You got a" + mark;
}
</script>
<br>
<p id="demo"></p>
</body>
</html>
答案 0 :(得分:2)
您没有使用正确的方法,请检查其工作原理here.
如果成绩变量是“F”,“D”,“C”.....你正在做的比较......
使用if
和else if
并将您的陈述放入其中,如下所示:
var mark = document.getElementById('mark').value // gets the input value to "mark" variable
if(mark >= 70 && mark <= 79.9){
// do something here
} else if(mark >= 80 && mark <= 89.9){
// do another thing here
}
等......
答案 1 :(得分:0)
见:
<body>
<h1>Rank Your Mark!!</h1>
Enter your mark to rank it.
<input type="number" name="grade" id="mark">
<input type="button" name="mark" id="grade" value="Rate" onclick="rateMark()">
<script>
function rateMark(){
mark = document.getElementById("mark").value
if (mark < 50) {
mark = "F"
} else if (mark >= 50 && mark <=59.9) {
mark = "E"
} else if (mark >= 60 && mark <=69.9) {
mark = "D"
} else if (mark >= 70 && mark <= 79.9) {
mark = "C"
} else if (mark >= 80 && mark <= 89.9) {
mark = "B"
} else if (mark >= 90 && mark <= 100) {
mark = "A"
}
document.getElementById("demo").innerHTML = "You got a " + mark;
}
</script>
<br>
<p id="demo"></p>
</body>