This question already has an answer here:
这个问题在这里已有答案:
- Test for existence of nested JavaScript object key 48 answers
测试是否存在嵌套的JavaScript对象键48个答案
var how = $('how');
var id = $('myId');
if ((how != null && !how.value.blank()) || myId != null) {
return true;
} else {
alert('Error');
}
Is there an easier way to check for not null and checking if the value of that element is blank without having to do both != null and then calling value?
是否有更简单的方法来检查not null并检查该元素的值是否为空而不必同时执行这两个操作!= null然后调用value?
1 个解决方案
#1
2
Since null
is falsy, a slightly shorter version would be
由于null是假的,因此稍微短一些
if((how && !how.value.blank()) || myId != null) {
...
}
Note that the above code and your own snippet both assume that if how
exists, it will have a property called value
, and will throw an exception if this is not the case.
请注意,上面的代码和您自己的代码段都假定如果存在,它将具有一个名为value的属性,并且如果不是这种情况将抛出异常。
#1
2
Since null
is falsy, a slightly shorter version would be
由于null是假的,因此稍微短一些
if((how && !how.value.blank()) || myId != null) {
...
}
Note that the above code and your own snippet both assume that if how
exists, it will have a property called value
, and will throw an exception if this is not the case.
请注意,上面的代码和您自己的代码段都假定如果存在,它将具有一个名为value的属性,并且如果不是这种情况将抛出异常。