如果使用jquery,请不要使用OR条件

时间:2014-11-03 07:00:09

标签: jquery

我正在使用jquery进行表单验证。但是我的if条件不起作用.contry3的值是'undefined'。 我的代码如下:

var country3=$('.shorttext2').val();
alert(country3);

 if(country3 =='' || country3 =='undefined')
 {
    alert("Please Add Country."); 
    return false;  
 }

有人帮帮我吗?

1 个答案:

答案 0 :(得分:1)

那应该是

if (typeof country3 == "undefined" || country3 =="") {

简单版本

 if(!country3)
 { 
     //not defined, or is empty
 }