为什么输入空格并确定或者直接点击取消,还是显示“要努力了”?应该如何优化if/else判断语句呢?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
<script type="text/javascript">
function rec(){
var score; //score变量,用来存储用户输入的成绩值。
score = prompt("输入成绩","请输入0-100的数值");
if(score>=90&&score<=100)
{
document.write("你很棒!");
}
else if(score>=75&&score<90)
{
document.write("不错吆!");
}
else if(score>=60&&score<75)
{
document.write("要加油!");
}
else if(score<60&&score>=0)
{
document.write("要努力了!");
}
else if(score == null)
{
document.write("点击取消了?");
}
else if(score == ' ')
{
document.write("输入空格了?");
}
else
{
alert("请输入正确分值");
}
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,对成绩做评价!" />
</body>
</html>