jQuery:检查字段的值是否为空(空) [英] jQuery: checking if the value of a field is null (empty)

查看:27
本文介绍了jQuery:检查字段的值是否为空(空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is this a good way to check if the value of a field is null?

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

或者有更好的方法吗?

推荐答案

一个字段的值不能为空,它总是一个字符串值.

The value of a field can not be null, it's always a string value.

代码将检查字符串值是否为字符串NULL".你想检查它是否是一个空字符串:

The code will check if the string value is the string "NULL". You want to check if it's an empty string instead:

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

或:

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

如果你想检查元素是否存在,你应该在调用 val 之前这样做:

If you want to check if the element exist at all, you should do that before calling val:

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

这篇关于jQuery:检查字段的值是否为空(空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆