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

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

问题描述

  if($('#person_data)是一个检查字段值为空的好方法吗?或者是否有一个[document_type]')。value()!='NULL'){} 

更好的方式?

解决方案

一个字段的值不能为null,它总是一个字符串值。
$ b

代码将检查字符串值是否为字符串NULL。您需要检查它是否为空字符串:

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

或者:

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

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

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


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

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

Or is there a better way?

解决方案

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

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() != ''){}

or:

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

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天全站免登陆