필드 값이 있는지 확인하는 좋은 방법 null입니까?
if($('#person_data[document_type]').value() != 'NULL'){}
아니면 더 좋은 방법이 있습니까?
1
#person_data는 어떤 요소입니까? 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 ) {...}
}
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요소는 당신이 일반적으로 가지고있는 것이 아닙니다. 꽤 쓸모가 없습니다.
<select>은 <option>어떤 종류의 유효성 검사에서 오는 사용자에게 가능한 옵션을 포함 할 수 있습니다 .
select시작됩니다 null. stackoverflow.com/questions/5805059/…
조건부로 전달하는 정보의 종류에 따라 다릅니다.
때때로 귀하의 결과는 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;
}
},
이를 달성하기 위해이 도우미를 활용하십시오.
isStringNullOrEmptyIS isStringFalsey때문에 return !val;일 것이다
시험
if( this["person_data[document_type]"].value != '') {
console.log('not empty');
}
<input id="person_data[document_type]" value="test" />