Javascript if语句不起作用 [英] Javascript if statements not working

查看:166
本文介绍了Javascript if语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常直接我想做的事情:

Pretty straight-forward what I want to do:


  • 如果输入是 0 ,这意味着他们没有输入数字而且
    应该告诉你。

  • 当输入 7 ,应该说你做对了。

  • 还有别的,它应该告诉你你弄错了。

  • If the input is 0, it means that they didn't input a number and it should tell you so.
  • When the input is 7, it should say that you got it right.
  • Anything else, it should tell you that you got it wrong.

但无论输入是什么,它只输出7是正确的线,我无法弄清楚出了什么问题。

But it just outputs the "7 is correct" line no matter what the input is, and I can't figure it out what is wrong.

<script type="text/javascript">
function problem2 ()
{
var number = 0;
var text=document.getElementById("output");
number = prompt("Enter a number between 1 and 10 please" , 0);
if (number = 0)
    {
     text.value = "You didn't enter a number!";
    }
if (number = 7)
    {
     text.value = "7 is correct!";
    }
else
    {
     text.value = "Sorry, ", input, "is not correct!";
    }
}
</script>
<input type="button" value="Click here" onclick="problem2()">
<input id="output" type="text">


推荐答案

您正在使用进行分配= 。使用 == ===

if( 0 == number ){

  text.value = "You didn't enter a number!";
}

另外,要小心你的支撑位置。 Javascript喜欢自动添加分号到行尾。 来源

Also, be wary of your brace placement. Javascript likes to automatically add semicolons to the end of lines. Source.

这篇关于Javascript if语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆