반응형
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" />
나의 작업 솔루션은
var town_code = $('#town_code').val();
if(town_code.trim().length == 0){
var town_code = 0;
}
당신의 코드에서'NULL'는 문자열입니다.그래서 틀렸습니다.
느낌표를 사용할 수 있습니다.!Null인지 확인합니다.
코드는 다음과 같습니다.
if(!$('#person_data[document_type]').val()){
}
위의 코드는 다음과 같은 경우를 의미합니다.$('#person_data[document_type]')값이 없습니다(값이 null인 경우).
언급URL : https://stackoverflow.com/questions/4244565/jquery-checking-if-the-value-of-a-field-is-null-empty
반응형
'programing' 카테고리의 다른 글
| 매개 변수가 있는 저장 프로시저 (0) | 2023.08.14 |
|---|---|
| 승인되지 않은 단순 Rest 쿼리를 실행하는 동안 예기치 않은 오류가 발생했습니다. (0) | 2023.08.14 |
| jquery를 사용하여 드롭다운 읽기만 만드는 방법은 무엇입니까? (0) | 2023.08.14 |
| 히카리 CP를 데비안 10으로 업그레이드 (마리아드브) (0) | 2023.08.14 |
| 코어 레벨 또는 ORM 레벨에서만 SQLAlchemy SQL 주입 보호 (0) | 2023.08.14 |