As you can see in the picture the value of the r.SalaryGrade
is null
. Why it skip my IF
condition and goes to Else
?. I make sure that if is an empty string and null it will add a class to a textbox. Did I do something wrong?.
if (r.SalaryGrade == "" && r.SalaryGrade == null) { $("#JobGradeId").addClass('validation'); }
else { $("#JobGradeId").removeClass('validation'); }
As you can see in the picture the value of the r.SalaryGrade
is null
. Why it skip my IF
condition and goes to Else
?. I make sure that if is an empty string and null it will add a class to a textbox. Did I do something wrong?.
if (r.SalaryGrade == "" && r.SalaryGrade == null) { $("#JobGradeId").addClass('validation'); }
else { $("#JobGradeId").removeClass('validation'); }
Share
Improve this question
asked Jul 4, 2017 at 1:22
KiRaKiRa
9243 gold badges19 silver badges46 bronze badges
4
-
1
I'm pretty sure in Javascript
'' != null
, in other words that if statement will never be entered as you want it to be an empty string and null. – Chris Commented Jul 4, 2017 at 1:24 -
1
Maybe swap and-
&&
for or-||
? – NewToJS Commented Jul 4, 2017 at 1:25 -
2
if (r.SalaryGrade == "" || r.SalaryGrade == null)
– cs95 Commented Jul 4, 2017 at 1:26 - Read it out loud. Grade equal to empty string AND Grade equal to null. – epascarello Commented Jul 4, 2017 at 1:35
3 Answers
Reset to default 2Your if condition has an "&&" in it.
'' and null are different things
You need to change how you structure that if statement. You could do something like,
r.SalaryGrade == "" || r.SalaryGrade == null
r.salaryGrade = "" as the first conditional passed, therefore it cannot be equal to null ( as it HAS a value of "" which is not null ).
You can change the && to an || and if either is true it will continue on.
As there is && in the if statement. it does not go in the if statement. Just try this for clarification- just make an if statement
String s= "";
IF(s == null) print "it is null";
else print "it is not null";
See yourself whether String s can be empty and null at the same time.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745090854a4610685.html
评论列表(0条)