IT

jQuery : 필드 값이 null (비어 있음)인지 확인

lottoking 2020. 8. 29. 16:10
반응형

jQuery : 필드 값이 null (비어 있음)인지 확인


필드 값이 있는지 확인하는 방법 null입니까?

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

아니면 더 좋은 방법이 있습니까?


필드의 값은 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 ) {...}
}

또한 입력 필드를 다듬어 공백이 채워져 보이게 할 수 있습니다.

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

}

가정

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)

따라서 필요한 것을 사용하십시오.


조건부로 전달하는 정보의 종류에 따라 달라집니다.

귀하의 결과 때때로는 null또는 undefined또는 ''또는 0일을 구석으로입니다. 나는 간단한 유효성 검사를 위해 사용합니다.

( $('#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;
        }
    },

이를 달성하기 위해이 도우미를 활용하십시오.


jquery는 val()기능과 not value(). jquery를 사용하여 빈 문자열을 확인할 수 있습니다.

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

시험

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

참고 URL : https://stackoverflow.com/questions/4244565/jquery-checking-if-the-value-of-a-field-is-null-empty

반응형