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

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

我不想重复

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

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


当前回答

if(!my_string){ 
// stuff 
}

and

if(my_string !== "")

如果你想接受空值但拒绝空值

编辑:哎呀,忘记你的条件是如果它是空的

其他回答

试试这个

if(!a || a.length === 0)

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

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

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

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

if(string) {
    // string is not empty
}
if((a.trim()=="")||(a=="")||(a==null))
{
    //empty condition
}
else
{
    //working condition
}
if(!my_string){ 
// stuff 
}

and

if(my_string !== "")

如果你想接受空值但拒绝空值

编辑:哎呀,忘记你的条件是如果它是空的

if (!a) {
  // is emtpy
}

忽略字符串中的空格:

if (!a.trim()) {
    // is empty or whitespace
}

如果您需要trim()的遗留支持(IE8-),请使用$。镶边或填充物