简体   繁体   English

javascript If-Else语句跳过If条件

[英]javascript If-Else Statement skip If condition

As you can see in the picture the value of the r.SalaryGrade is null . 如您在图片中看到的, r.SalaryGrade值为null Why it skip my IF condition and goes to Else ?. 为什么它跳过我的IF条件并转到Else I make sure that if is an empty string and null it will add a class to a textbox. 我确保如果为空字符串且为null,它将为文本框添加一个类。 Did I do something wrong?. 我做错什么了吗?。

 if (r.SalaryGrade == "" && r.SalaryGrade == null) { $("#JobGradeId").addClass('validation'); }
 else { $("#JobGradeId").removeClass('validation'); }

在此处输入图片说明

Your if condition has an "&&" in it. 您的if条件中包含“ &&”。

'' and null are different things

You need to change how you structure that if statement. 您需要更改if语句的结构。 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 ). r.salaryGrade =“”作为第一个条件传递,因此它不能等于null(因为它的值“”不为null)。

You can change the && to an || 您可以将&&更改为||。 and if either is true it will continue on. 如果这是真的,它将继续。

As there is && in the if statement. 因为if语句中有&&。 it does not go in the if statement. 它不在if语句中。 Just try this for clarification- just make an if statement 只需尝试进行澄清-只需执行if语句

String s= ""; 字符串s =“”;

IF(s == null) print "it is null"; IF(s == null)打印“它为空”;

else print "it is not null"; 否则打印“它不为空”;

See yourself whether String s can be empty and null at the same time. 看看自己String是否可以同时为空和null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM