If case is always true, but probably false

后端 未结 1 1001
清酒与你
清酒与你 2020-12-22 02:13

I have a simple form drop-down, and I want to display something different depending on the selection value. I have a variable called connectiontype that comes in wi

相关标签:
1条回答
  • 2020-12-22 02:24

    You are using = (assignment) instead of == (comparison).

    if (connectiontype == 'red') {
        ...
    } else if (connectiontype == 'green') {
        ...
    }
    

    When you have an assignment, eg: lhs = rhs the whole expression returns whatever rhs is. So:

    if (connectiontype = 'red') { ...
    
    // is equivalent to (as far as the "if" is concerned):
    
    if ('red') { ...  
    

    Since 'red' (a non-empty string) is "truthy" in JavaScript, the if is always true and your html variable will always be set to 'Red'.

    0 讨论(0)
提交回复
热议问题