这是检查字段值是否为的好方法null吗?
if($('#person_data[document_type]').value() != 'NULL'){}或者,还有更好的方法?
                
                1
              
                  #person_data是哪种元素?您认为什么是NULL值?
                
                
              这是检查字段值是否为的好方法null吗?
if($('#person_data[document_type]').value() != 'NULL'){}或者,还有更好的方法?
Answers:
字段的值不能为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 ) {...}
}
The value of a field can not be null, it's always a string value.这并非完全正确。我有一个select没有的options。当我执行.val()此操作时,它会返回null。jQuery 1.7.2
                    select没有任何option元素的A 通常不是您所拥有的。真没用。
                    <select>您的用户的<option>s来自某种验证,则其空白可能包含选项。
                    这取决于您要传递给条件信息的类型。
有时您的结果将是nullor undefined或''or 0,对于我的简单验证,我使用if。
( $('#id').val() == '0' || $('#id').val() == '' || $('#id').val() == 'undefined' || $('#id').val() == null )注意:null!='null'
_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;
        }
    },
利用这些助手来实现这一目标。
isStringNullOrEmptyIS isStringFalsey,从而return !val;将工作
                    尝试
if( this["person_data[document_type]"].value != '') { 
  console.log('not empty');
}<input id="person_data[document_type]" value="test" />