Javascript null或空字符串不起作用

编程入门 行业动态 更新时间:2024-10-25 14:35:04
本文介绍了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或空字符串不起作用

本文发布于:2023-10-16 11:49:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   空字符串   Javascript   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!