Javascript null或空字符串不起作用 [英] Javascript null or empty string does not work

查看:100
本文介绍了Javascript null或空字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试我的字符串为空或空,但是它不起作用。

I am trying to test that my string is null or empty, however it does not work.

我的代码:

var veri = {
  YeniMusteriEkleTextBox: $('#MyTextbox').val(),
};

if (veri.YeniMusteriEkleTextBox === "" || 
    veri.YeniMusteriEkleTextBox == '' || 
    veri.YeniMusteriEkleTextBox.length == 0 || 
    veri.YeniMusteriEkleTextBox == null) {
  alert("Customer Name can not be empty!!!");
}

如何检查YeniMusteriEkleTextBox是空还是空?

How can ı check YeniMusteriEkleTextBox is null or empty ?

推荐答案

我会用的!运算符测试它是否为空,未定义等。

I would use the ! operator to test if it is empty, undefined etc.

if (!veri.YeniMusteriEkleTextBox) {
    alert("Customer Name can not be empty!!!");
}

此外,您不需要在 YeniMusteriEkleTextBox之后使用逗号: $('#MyTextbox')。val(),

同样测试可能未定义的对象的长度会引发错误因为长度不是0,所以它将是未定义的。

Also testing for a length on an object that may be undefined will throw an error as the length will not be 0, it will instead be undefined.

这篇关于Javascript null或空字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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