用开箱即用的jquery测试空字符串的最好方法是什么,即没有插件?我试过这个。

但至少在开箱即用时行不通。使用内置的东西会很好。

我不想重复

if (a == null || a=='')

where if some if (isempty(a))将可用。


当前回答

if((a.trim()=="")||(a=="")||(a==null))
{
    //empty condition
}
else
{
    //working condition
}

其他回答

用jQuery检查data是否为空字符串(并忽略任何空白):

function isBlank( data ) {
    return ( $.trim(data).length == 0 );
}

你给出的链接似乎在尝试与你试图避免重复的测试不同的东西。

if (a == null || a=='')

测试字符串是空字符串还是空字符串。您链接到的文章将测试字符串是否完全由空格组成(或为空)。

您描述的测试可以用以下方法代替:

if (!a)

因为在javascript中,空字符串和null在布尔上下文中的值都是false。

试试这个

if(!a || a.length === 0)
if((a.trim()=="")||(a=="")||(a==null))
{
    //empty condition
}
else
{
    //working condition
}

尝试在浏览器控制台或node.js repl中执行此命令。

var string = ' ';
string ? true : false;
//-> true

string = '';
string ? true : false;
//-> false

因此,一个简单的分支结构就足够测试了。

if(string) {
    // string is not empty
}