javascript 如果变量等于数字 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18110478/
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-10-27 10:45:25  来源:igfitidea点击:

If variable equals the number 1

javascriptvariablesif-statement

提问by loriensleafs

I'm trying to write a very simple if statement in my jquery. If the variable equals the number one then do this, if it equals the number two then do this. This is what I wrote but it doesn't seem to work and I can't figure out why:

我正在尝试在我的 jquery 中编写一个非常简单的 if 语句。如果变量等于数字 1,则执行此操作,如果它等于数字 2,则执行此操作。这是我写的,但它似乎不起作用,我不知道为什么:

$("#next-btn").click(function() {
    if (current_slide = 1) {
        first_screen();
    }, else if (current_slide = 2) {
        second_screen();
    } 
});

probably very simple, but I appreciate the help.

可能很简单,但我很感激你的帮助。

回答by Adil

You need to use comparison operator==in if statementcondition instead of assignment operator =also remove commaafter first closing curly bracket of then(true) block of ifstatement. You can test it over here.

您需要在if 语句条件中使用比较运算符而不是赋值运算符,并且在(true)语句块的第一个关闭大括号后也删除。你可以在这里测试它。===commathenif

if (current_slide == 1) {
      first_screen();
} else if (current_slide == 2) {
      second_screen();
} 

I assume current_slide has some number to compare, you read below how comparison operator == performs the comparison.

我假设 current_slide 有一些要比较的数字,您在下面阅读了比较运算符 == 如何执行比较。

Comparion equal operator

比较等号运算符

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory, reference.

如果两个操作数的类型不同,JavaScript 会转换操作数,然后应用严格的比较。如果操作数是数字或布尔值,则尽可能将操作数转换为数字;else 如果任一操作数是字符串,则另一个操作数在可能的情况下转换为字符串。如果两个操作数都是对象,然后JavaScript的比较内部引用时的操作数是指在存储器中的相同对象,这是相等的参考

回答by Nishu Tayal

Use comparison operator "==" instead of assignment "=" and remove comma before else if.

使用比较运算符“==”而不是赋值“=”并在else if.

$("#next-btn").click(function() {
    if (current_slide == 1) {
        first_screen();
    }else if (current_slide == 2) {
        second_screen();
    } 
});

回答by falinsky

May be it'd be better using switch:

使用 switch 可能会更好:

$("#next-btn").click(function() {
    switch(current_slide) {
        case 1:
            first_screen();
            break;
        case 2:
            second_screen();
            break;            
    }
});

回答by lfergon

Youd shouldn′t check a condition with an assigment:

你不应该用赋值来检查条件:

$("#next-btn").click(function() {
    if(current_slide===1){
        first_screen();
    }else if (current_slide===2){
        second_screen();
    } 
});