Javascript 如何在 jQuery 中检查“未定义”值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10653367/
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
How to check 'undefined' value in jQuery
提问by Gaurav
Possible Duplicate:
Detecting an undefined object property in JavaScript
javascript undefined compare
How we can add a check for an undefined variable, like:
我们如何添加对未定义变量的检查,例如:
function A(val) {
if (val == undefined)
// do this
else
// do this
}
回答by VisioN
JQuery library was developed specifically to simplify and to unify certain JavaScript functionality.
JQuery 库是专门为简化和统一某些 JavaScript 功能而开发的。
However if you need to check a variable against undefined
value, there is no need to invent any special method, since JavaScript has a typeof
operator, which is simple, fast and cross-platform:
但是,如果您需要根据undefined
值检查变量,则无需发明任何特殊方法,因为 JavaScript 有一个typeof
操作符,它简单、快速且跨平台:
if (typeof value === "undefined") {
// ...
}
It returns a string indicating the type of the variable or other unevaluated operand. The main advantage of this method, compared to if (value === undefined) { ... }
, is that typeof
will never raise an exception in case if variable value
does not exist.
它返回一个字符串,指示变量或其他未计算的操作数的类型。与 相比,此方法的主要优点if (value === undefined) { ... }
是,typeof
如果变量value
不存在,则永远不会引发异常。
回答by ThiefMaster
In this case you can use a === undefined
comparison: if(val === undefined)
在这种情况下,您可以使用=== undefined
比较:if(val === undefined)
This works because val
always exists(it's a function argument).
这是有效的,因为它val
始终存在(它是一个函数参数)。
If you wanted to test an arbitrary variable that is not an argument, i.e. might not be defined at all, you'd have to use if(typeof val === 'undefined')
to avoid an exception in case val
didn't exist.
如果你想测试一个不是参数的任意变量,即可能根本没有定义,你必须使用if(typeof val === 'undefined')
来避免异常,以防val
不存在。
回答by Jignesh Rajput
Note that typeof always returns a string, and doesn't generate an error if the variable doesn't exist at all.
请注意, typeof 始终返回一个字符串,如果该变量根本不存在,则不会产生错误。
function A(val){
if(typeof(val) === "undefined")
//do this
else
//do this
}
回答by Arpita
I know I am late to answer the function but jquery have a in build function to do this
我知道我回答这个函数迟到了,但是 jquery 有一个 in build 函数来做到这一点
if(jQuery.type(val) === "undefined"){
//Some code goes here
}
Refer jquery API document of jquery.type https://api.jquery.com/jQuery.type/for the same.
参考 jquery.type https://api.jquery.com/jQuery.type/ 的jquery API 文档。
回答by Rohidas Kadam
You can use shorthand technique to check whether it is undefined or null
您可以使用速记技术来检查它是否为 undefined 或 null
function A(val)
{
if(val || "")
//do this
else
//do this
}
hope this will help you
希望能帮到你
回答by sandeep kumar
if (value === undefined) {
// ...
}
回答by Sridhar K
when I am testing "typeof obj === undefined
", the alert(typeof obj)
returning object
, even though obj is undefined.
Since obj is type of Object
its returning Object
, not undefined
.
当我测试“ typeof obj === undefined
”时,即使 obj 未定义,也会alert(typeof obj)
返回object
。由于 obj 是Object
其返回的类型Object
,而不是undefined
.
So after hours of testing I opted below technique.
因此,经过数小时的测试,我选择了以下技术。
if(document.getElementById(obj) !== null){
//do...
}else{
//do...
}
I am not sure why the first technique didn't work.But I get done my work using this.
我不确定为什么第一种技术不起作用。但是我使用它完成了我的工作。
回答by esdu
I am not sure it is the best solution, but it works fine:
我不确定这是最好的解决方案,但它工作正常:
if($someObject['length']!=0){
//do someting
}
回答by MGK
function isValue(value, def, is_return) {
if ( $.type(value) == 'null'
|| $.type(value) == 'undefined'
|| $.trim(value) == ''
|| ($.type(value) == 'number' && !$.isNumeric(value))
|| ($.type(value) == 'array' && value.length == 0)
|| ($.type(value) == 'object' && $.isEmptyObject(value)) ) {
return ($.type(def) != 'undefined') ? def : false;
} else {
return ($.type(is_return) == 'boolean' && is_return === true ? value : true);
}
}
try this~ all type checker
试试这个~所有类型检查器
回答by Yoosaf Abdulla
If you have names of the element and not id we can achieve the undefined check on all text elements (for example) as below and fill them with a default value say 0.0:
如果您有元素的名称而不是 id,我们可以实现对所有文本元素(例如)的未定义检查,如下所示,并用默认值填充它们,例如 0.0:
var aFieldsCannotBeNull=['ast_chkacc_bwr','ast_savacc_bwr'];
jQuery.each(aFieldsCannotBeNull,function(nShowIndex,sShowKey) {
var $_oField = jQuery("input[name='"+sShowKey+"']");
if($_oField.val().trim().length === 0){
$_oField.val('0.0')
}
})