Javascript if 语句不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13851399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 14:56:21  来源:igfitidea点击:

Javascript if statements not working

javascriptif-statement

提问by xjsc16x

Pretty straight-forward what I want to do:

我想要做的非常简单:

  • 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.
  • 如果输入是0,则意味着他们没有输入数字,它应该告诉你。
  • 当输入是 时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.

但无论输入是什么,它都只输出“7 是正确的”行,我无法弄清楚什么是错的。

<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">

回答by Josh

You're assigning with =. Use ==or ===.

您正在分配=. 使用=====

if( 0 == number ){

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

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

另外,请注意您的支具位置。Javascript 喜欢在行尾自动添加分号。来源

回答by AlienWebguy

You are using assignment operators as your conditionals instead of comparison operators:

您使用赋值运算符作为条件而不是比较运算符:

if (number = 0) // falsy. Same as if (false)
    {
     text.value = "You didn't enter a number!";
    }
if (number = 7) // truthy. Same as if (true)
    {
     text.value = "7 is correct!";
    }
else
    {
     text.value = "Sorry, ", input, "is not correct!";
    }

Alternatively you can use a switch and organize the conditionals a bit easier:

或者,您可以使用 switch 并更轻松地组织条件:

switch (number) {
    case 0: 
        text.value = "You didn't enter a number!";
        break;

    case 7:
        text.value = "7 is correct!";
        break;

    default:
        text.value = "Sorry, ", input, "is not correct!";
        break;
}

回答by ajax333221

Here is a code with the some fixes and improvements (I commented what I changed):

这是一个包含一些修复和改进的代码(我评论了我所做的更改):

function problem2 (){
    //I multiplied by * 1 to work with numbers, also used || to default to 0 in case of NaN
    var num = (prompt("Enter a number between 1 and 10 please" , 0) * 1) || 0;
    var msg = "";

    if (!num){ //I prefer this over 'num == 0'
         msg = "You didn't enter a number!";
    //you should use 'else if' in this case
    }else if (num == 7){//'=' is for assignment, use '==' or '===' instead
         msg = "7 is correct!";
    }else{
        //you had an undefined var 'input', you probably meant 'num'
        //you also were connecting var and strings using commas, use '+' instead
         msg = "Sorry, " + num + " is not correct!"; //added a space in ' is'
    }

    //no need to store the element in a var anymore :D
    document.getElementById("output").value = msg;
}

Aditionally, two more changes can be made:

此外,还可以进行另外两项更改:

  • only one var(e.g var something = "", somethingElse = 99;)
  • assign the default text from the beginning, like var msg = "default"and remove the else
  • 只有一个var(例如var something = "", somethingElse = 99;
  • 从头开始分配默认文本,喜欢var msg = "default"并删除else

Note:an undocumented change I made was to rename some vars, I encourage everyone to stop using vars like number, text, string, if you have this bad habit, you will eventually use illegal var names by mistake.

注意:我所做的一个未公开的更改是重命名一些 vars,我鼓励大家停止使用 vars 之类的number, text, string,如果你有这个坏习惯,你最终会错误地使用非法 var 名称。