我正在尝试测试我的字符串是否为 null 或空,但它不起作用。
我的代码:
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 是否为空?
最佳答案
我会用!运算符来测试它是否为空、未定义等。
if (!veri.YeniMusteriEkleTextBox) {
alert("Customer Name can not be empty!!!");
}
此外,YeniMusteriEkleTextBox: $('#MyTextbox').val(),
同时在可能 undefined object 上测试长度将引发错误,因为长度不会为 0,而是未定义。
关于Javascript null 或空字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18765055/