Javascript 如何在Javascript中检查空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6997187/
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 for empty value in Javascript?
提问by sqlmole
I am working on a method of retrieving an array of hidden inputs in my form like so
我正在研究一种以我的形式检索隐藏输入数组的方法
<input type="hidden" value="12:34:00" name="timetemp0">
<input type="hidden" value="14:45:00" name="timetemp1">
<input type="hidden" value="15:12:00" name="timetemp2">
<input type="hidden" value="16:42:12" name="timetemp3">
<input type="hidden" value="16:54:56" name="timetemp4">
<input type="hidden" value="17:03:10" name="timetemp5">
My javascript function retrieves these individually by using getElementsByName('timetemp'+i)
我的 javascript 函数使用 getElementsByName('timetemp'+i) 单独检索这些
for (i ; i < counter[0].value; i++)
{
//finds hidden element by using concatenation of base name plus counter
var timetemp = document.getElementsByName('timetemp'+i);
//if there is a value alert that value to user - this is just for testing purposes at the moment
//because there is only one of timetemp.i then it occupies position 0 in array
if (timetemp[0].value == null)
{
alert ('No value');
}
else
{
alert (timetemp[0].value);
}
}
So what should happen is it will alert the user of the value in that hidden input but if it comes accross an input with no value like this:
所以应该发生的是它会提醒用户该隐藏输入中的值,但如果它遇到一个没有值的输入,如下所示:
<input type="hidden" value="" name="timetemp16">
Then it will say "No value"
然后它会说“没有价值”
However th if function cannot seem to work with this:
但是,如果功能似乎无法与此一起使用:
I have tried:
我试过了:
(timetemp[0].value == null)
(timetemp[0].value === null)
(timetemp[0].value == undefined)
(timetemp[0].value == '')
(timetemp[0].value == null)
(timetemp[0].value === null)
(timetemp[0].value == undefined)
(timetemp[0].value == '')
It always seems to default to else clause.
它似乎总是默认为 else 子句。
Any ideas?
有任何想法吗?
回答by Richard Dalton
Comment as an answer:
评论作为答案:
if (timetime[0].value)
This works because any variable in JS can be evaluated as a boolean, so this will generally catch things that are empty, null, or undefined.
这是有效的,因为 JS 中的任何变量都可以作为布尔值进行评估,因此这通常会捕获空、空或未定义的内容。
回答by Jamter
In my opinion, using "if(value)" to judge a value whether is an empty value is not strict, because the result of "v?true:false" is false when the value of v is 0(0 is not an empty value). You can use this function:
在我看来,使用“if(value)”来判断一个值是否为空值并不严格,因为当v的值为0时,“v?true:false”的结果为false(0不是空的)价值)。您可以使用此功能:
const isEmptyValue = (value) => {
if (value === '' || value === null || value === undefined) {
return true
} else {
return false
}
}
回答by FishBasketGordo
First, I would check what i
gets initialized to, to see if the elements returned by getElementsByName
are what you think they are. Maybe split the problem by trying it with a hard-coded name like timetemp0
, without the concatenation. You can also run the code through a browser debugger (FireBug, Chrome Dev Tools, IE Dev Tools).
首先,我会检查什么i
被初始化,看看返回的元素getElementsByName
是否是你认为的那样。也许通过尝试使用硬编码名称(如timetemp0
,而不是串联)来解决问题。您还可以通过浏览器调试器(FireBug、Chrome 开发工具、IE 开发工具)运行代码。
Also, for your if-condition, this should suffice:
此外,对于您的 if 条件,这应该足够了:
if (!timetemp[0].value) {
// The value is empty.
}
else {
// The value is not empty.
}
The empty string in Javascript is a falsey value, so the logical negation of that will get you into the if-block.
Javascript 中的空字符串是一个 falsey 值,因此逻辑否定将使您进入 if 块。
回答by Jason Gennaro
Your script seems incorrect in several places.
您的脚本在几个地方似乎不正确。
Try this
尝试这个
var timetemp = document.getElementsByTagName('input');
for (var i = 0; i < timetemp.length; i++){
if (timetemp[i].value == ""){
alert ('No value');
}
else{
alert (timetemp[i].value);
}
}
Example: http://jsfiddle.net/jasongennaro/FSzT2/
示例:http: //jsfiddle.net/jasongennaro/FSzT2/
Here's what I changed:
这是我改变的:
- started by getting all the
input
s viaTagName
. This makes an array - initialized
i
with avar
and then looped through thetimetemp
array using thetimetemp.length
property. - used
timetemp[i]
to reference eachinput
in thefor statement
- 通过获取所有
input
s开始TagName
。这使得一个数组 i
用 a初始化,var
然后timetemp
使用timetemp.length
属性循环遍历数组。- 用于
timetemp[i]
引用每个input
在for statement
回答by James Hill
The counter in your for loop appears incorrect (or we're missing some code). Here's a working Fiddle.
for 循环中的计数器似乎不正确(或者我们缺少一些代码)。这是一个有效的 Fiddle。
This code:
这段代码:
//Where is counter[0] initialized?
for (i ; i < counter[0].value; i++)
Should be replaced with:
应替换为:
var timeTempCount = 5; //I'm not sure where you're getting this value so I set it.
for (var i = 0; i <= timeTempCount; i++)