Javascript jQuery:检查字段的值是否为空(空)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4244565/
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
jQuery: checking if the value of a field is null (empty)
提问by ziiweb
Is this a good way to check if the value of a field is null
?
这是检查字段值是否为的好方法null
吗?
if($('#person_data[document_type]').value() != 'NULL'){}
Or is there a better way?
或者,还有更好的方法?
回答by Guffa
The value of a field can not be null, it's always a string value.
字段的值不能为空,它总是一个字符串值。
The code will check if the string value is the string "NULL". You want to check if it's an empty string instead:
代码将检查字符串值是否为字符串“NULL”。你想检查它是否是一个空字符串:
if ($('#person_data[document_type]').val() != ''){}
or:
或者:
if ($('#person_data[document_type]').val().length != 0){}
If you want to check if the element exist at all, you should do that before calling val
:
如果你想检查元素是否存在,你应该在调用之前这样做val
:
var $d = $('#person_data[document_type]');
if ($d.length != 0) {
if ($d.val().length != 0 ) {...}
}
回答by Flipke
I would also trim the input field, cause a space could make it look like filled
我也会修剪输入字段,因为空格可能使它看起来像填充
if ($.trim($('#person_data[document_type]').val()) != '')
{
}
回答by darioo
Assuming
假设
var val = $('#person_data[document_type]').value();
you have these cases:
你有这些情况:
val === 'NULL'; // actual value is a string with content "NULL"
val === ''; // actual value is an empty string
val === null; // actual value is null (absence of any value)
So, use what you need.
所以,使用你需要的。
回答by Allan Felipe Murara
that depends on what kind of information are you passing to the conditional..
这取决于您将什么样的信息传递给条件..
sometimes your result will be null
or undefined
or ''
or 0
, for my simple validation i use this if.
有时您的结果将是null
orundefined
或''
or 0
,为了我的简单验证,我使用这个 if。
( $('#id').val() == '0' || $('#id').val() == '' || $('#id').val() == 'undefined' || $('#id').val() == null )
NOTE: null
!= 'null'
注意:null
!='null'
回答by Nilesh Moradiya
_helpers: {
//Check is string null or empty
isStringNullOrEmpty: function (val) {
switch (val) {
case "":
case 0:
case "0":
case null:
case false:
case undefined:
case typeof this === 'undefined':
return true;
default: return false;
}
},
//Check is string null or whitespace
isStringNullOrWhiteSpace: function (val) {
return this.isStringNullOrEmpty(val) || val.replace(/\s/g, "") === '';
},
//If string is null or empty then return Null or else original value
nullIfStringNullOrEmpty: function (val) {
if (this.isStringNullOrEmpty(val)) {
return null;
}
return val;
}
},
Utilize this helpers to achieve that.
利用这个助手来实现这一目标。
回答by Kamil Kie?czewski
Try
尝试
if( this["person_data[document_type]"].value != '') {
console.log('not empty');
}
<input id="person_data[document_type]" value="test" />
回答by Chinmayee G
jquery provides val()
function and not value()
. You can check empty string using jquery
jquery 提供val()
函数和not value()
. 您可以使用 jquery 检查空字符串
if($('#person_data[document_type]').val() != ''){}