繁体   English   中英

jQuery使用!=检查是否不是null / undefined / empty无效

[英]jQuery check if not null/undefined/empty with != doesn't work

我有这个jQuery代码

if (date != !date) {
    console.log(date);
}

date是一个数组,或者为null 如果它是一个数组,我要记录它,如果它为null我想就此停止它。 我认为!= !var正是用于此目的。 仍然,当我尝试这样做时,我也会得到null控制台日志。 怎么会?

x始终不等于!x(这就是x!= !x含义)。

您想要类似:x是否存在? 它为空吗?

if (date != null) {
    console.log(date);
}

 var x1; var x2 = [1,2]; if(x1 != null) // <- false console.log(x1); if(x2 != null) // <- true console.log(x2); 

试试这个,它应该抓住其他一切...

if(Array.isArray(date)){
  console.log(date); 
}
else {
  console.log('not array');
}

尝试这个:

if (date){
    console.log(date);
}

因此,您需要找出某个值是否为数组。 这是ECMAScript标准建议的另一种方法。 有关此的更多信息,请参见这篇文章: 检查对象是否为数组?

 var date = ['one', 'two', 'three']; var txt = "bla ... bla ..."; if( Object.prototype.toString.call( date ) === '[object Array]' ) { console.log('is array'); } else { console.log(' not an array'); } if( Object.prototype.toString.call( txt ) === '[object Array]' ) { console.log('is array'); } else { console.log('is not an array'); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM