Javascript String Compare == 有时会失败
为什么下面的代码有时会计算为 false?
(transport.responseText == '1' ||
transport.responseText == 'CARD_VALID')
我的 JavaScript 代码:
if (transport.responseText == '1' ||
transport.responseText == 'CARD_VALID') {
// do something.
}
else if (transport.responseText == 'CARD_INVALID' ||
transport.responseText == 'INVALID_CHECKSUM') {
// do something else....
}
else {
new Ajax.Request('/report_error.php?responseText='+transport.responseText);
// report error to user
}
当字符串相同时,什么会导致 JavaScript 字符串比较 ==
返回 false?
How could the following code sometimes evaluate to false?
(transport.responseText == '1' ||
transport.responseText == 'CARD_VALID')
My JavaScript code:
if (transport.responseText == '1' ||
transport.responseText == 'CARD_VALID') {
// do something.
}
else if (transport.responseText == 'CARD_INVALID' ||
transport.responseText == 'INVALID_CHECKSUM') {
// do something else....
}
else {
new Ajax.Request('/report_error.php?responseText='+transport.responseText);
// report error to user
}
What could cause JavaScript string compare ==
to return false when the strings are identical?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Double equals 是在 Javascript 中比较字符串的适当方法,它返回 false,则一个字符串的左侧和/或右侧可能有空格。
将
.trim()
放在字符串末尾,我的比较应该开始工作:Double equals is the appropriate way to compare strings in Javascript, it is returning false then there may be whitespace to the left and or right of one string.
Put a
.trim()
on the end of the strings and my comparison should started working:在控制台中使用此测试:
使用正确的代码:
USE THIS TEST IN CONSOLE:
Use correct code:
我遇到了一个类似的问题,两个明显相同的字符串不相等,我费尽心思试图解决它,所以我这样做了:
我发现一个字符串上的最后一个字符是 10,而一个字符串上的最后一个字符是 10。另一个是 13,我以为两个字符串都是空终止的,但事实并非如此。
I had a similar problem where two obviously identical strings would not be equal, and I was pulling my hair out trying to solve it, so I did this:
And I found that the last character on one string was 10, and the last character on the other was 13, I thought both strings were null terminated, but they were not.
我遇到了同样的问题,我注意到我正在比较两个对象
为了解决这个问题,我必须使用
I had the same problem and i noticed that i was comparing two objects
to solve this issue i had to use
尝试使用
===
来精确匹配(类型和值)。 这是 javascript 中推荐的比较运算符。检查字符串的数据类型以确保确定,并在两个字符串中查找隐藏的 unicode 或控制字符。
Try using
===
to match exactly (type and value). This is the recommended comparison operator in javascript.Check the datatypes of the strings to make sure, and look for hidden unicode or control characters in both strings.
我建议您使用 标准化最好是“NFKC”或“NFKD”,因为它们似乎将不间断空间标准化为常规空间。
所以你可以将代码编写为:-
I would advice you to use normalization preferably "NFKC" or "NFKD" as these seem to normalize non-breaking space into regular space.
So you can write your code as :-
如果你想要一些不那么复杂的东西并且你正在处理数字值,那么使用
就像一个魅力
If you want something a little less complicated and you are dealing with NUMERIC VALUES, use
works like a charm
在进入该代码块之前,尝试将
responseText
的值捕获到不同的变量中,以防变量在其中的某处更新。我没有太多直接使用 XmlHttpRequest 的经验,但我确实知道 javascript 有很多地方使用对在执行期间可以更改的接口对象的易失性引用,而不是简单的值。
Try capturing the value of
responseText
into a different variable before entering that code block, in case the variable is updated somewhere in there.I don't have that much experience directly using XmlHttpRequest, but I do know that javascript has a number of places where it uses volatile references to interface objects that can change during execution, rather than a simple value.
选角对我有用
casting worked for me
Java servlet 可以发送字符串,即
out.println("CARD_VALID");
或
out.print("CARD_VALID");
这些在 Javascript 中可能看起来相同,但在第一种情况下末尾有空格。
Java servlet may send strings i.e.
out.println("CARD_VALID");
or
out.print("CARD_VALID");
These may look identical in Javascript, but there are white spaces at the end in the first case.