jQuery: 检查字段值是否为空(null)

120

这是检查字段值是否为null的好方法吗?

if($('#person_data[document_type]').value() != 'NULL'){}

或者有更好的方法吗?


1
#person_data是什么类型的元素?你认为什么是NULL值? - fcalderan
https://dev59.com/0mMk5IYBdhLWcg3w8iWC#49923589 - Billu
9个回答

195

字段的值不能为null,它总是一个字符串值。

代码将检查字符串值是否为字符串“NULL”。你需要检查是否为空字符串:

if ($('#person_data[document_type]').val() != ''){}

或者:
if ($('#person_data[document_type]').val().length != 0){}

如果你想检查元素是否存在,应在调用 val 之前进行检查:

var $d = $('#person_data[document_type]');
if ($d.length != 0) {
  if ($d.val().length != 0 ) {...}
}

34
一个字段的值不能为null,它始终是一个字符串值,这并不完全正确。我有一个没有任何选项的select。当我对其执行.val()时,它返回null。Jquery 1.7.2 - Liam
@Pispirulito:我不这么认为,也不认为它会改变。一个没有任何option元素的select并不是你通常拥有的东西。它非常无用。 - Guffa
1
@Guffa,不太同意您的观点。一个空的<select>可以包含可能选项,这些选项的<option>将来自某种验证。 - Malcolm Salvador
如果您使用以下解决方案为您的 select 提供占位符,它将从一个“禁用”选项开始,因此具有 null 值。https://dev59.com/5m025IYBdhLWcg3wyI-V - Sygmoral
这对我有用,不等于空值 if ($('#person_data[document_type]').val() !== null){} - SWAT 10101

38

我还会对输入字段进行修剪,因为空格可能会使其看起来已经填好了。

if ($.trim($('#person_data[document_type]').val()) != '')
{

}

17
假设
var val = $('#person_data[document_type]').value();

你有以下几种情况:

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)

所以,使用你需要的。


15
这取决于您传递给条件语句的信息类型...
有时您的结果将是null、undefined、''或0,为了进行简单的验证,我使用这个if语句。
( $('#id').val() == '0' || $('#id').val() == '' || $('#id').val() == 'undefined' || $('#id').val() == null )

注意: null != 'null'


8
_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;
        }
    },

利用这些辅助工具可以实现此目标。

这似乎几乎是 isStringNullOrEmpty 就是 isStringFalsey,因此 return!val; 可以工作。 - Mark Schultheiss

0

尝试

if( this["person_data[document_type]"].value != '') { 
  console.log('not empty');
}
<input id="person_data[document_type]" value="test" />


0
在你的代码中,'NULL' 是一个字符串。所以是错误的。
你可以使用感叹号 ! 来检查它是否为空。
以下是代码:
if(!$('#person_data[document_type]').val()){

}

以上代码的意思是,如果 $('#person_data[document_type]') 没有值(即值为 null)。

0

jQuery 提供了 val() 函数和 not value()。您可以使用 jQuery 检查空字符串。

if($('#person_data[document_type]').val() != ''){}

0

我的工作解决方案是

var town_code = $('#town_code').val(); 
 if(town_code.trim().length == 0){
    var town_code = 0;
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接