【问题标题】:jquery - why if clause fires although condition is not met?jquery - 尽管条件不满足,为什么 if 子句会触发?
【发布时间】:2011-11-17 15:33:27
【问题描述】:

不知道怎么回事……

我有这个:

console.log("n="+n): 
console.log("longest.length="+longest.length);
console.log("longest.length/n="+longest.length/n);

if ( n=1 || longest.length/n != 1 ) { 
  // do something 
}

控制台显示 n=4longest.length=4longest.length/n=1

仍然...做一些火灾...我做错了什么?

【问题讨论】:

  • 为什么不longest.length == n

标签: javascript jquery if-statement operators


【解决方案1】:

尝试n==1 而不是n=1。您将n 设置为1,而不是检查它的值。

【讨论】:

  • 哦,是的...我应该在一小时前注意到...第一个答案。非常感谢。
【解决方案2】:
if ( n=1 || longest.length/n != 1 ) { 
  // do something 
}

应该是

if ( n==1 || longest.length/n != 1 ) { 
  // do something 
}

【讨论】:

    【解决方案3】:

    您的n=1 比较实际上是一个作业。尝试将其更改为:

    if ( n == 1 || longest.length/n != 1 ) { 
      // do something 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      相关资源
      最近更新 更多