javascript 比较javascript中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10411150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:47:25 来源:igfitidea点击:
Compare String in javascript
提问by Dani
In jquery ajax function I recive to the server "true" or "false" strings. Then I only wnat know result but only get false in if condition.
在 jquery ajax 函数中,我收到服务器“真”或“假”字符串。然后我只知道结果,但只在 if 条件下得到错误。
$.ajax({
...
success: function(result) {
if(result == "true"){ // false???????
alert('true');
} else {
alert('false');
}
}
})
...
$.ajax({
...
success: function(result) {
var result2 = String(result);
alert(result2); // true
alert(typeof result2); // String
alert(typeof "true"); // String
if(result2 == "true"){ // false???????
alert('true');
} else {
alert('false');
}
}
})
...
...
...
Somebody can help me?
有人可以帮助我吗?
回答by Dandy
There could be line breaks or extra spaces in "result".
“结果”中可能有换行符或额外的空格。
回答by Dandy
use trim() function...
使用trim()函数...
e.g:
例如:
$.ajax({
$.ajax({
...
...
success: function(result) {
var result2 = String(result.trim());
alert(result2); // true
alert(typeof result2); // String
alert(typeof "true"); // String
if(result2 == "true"){ // true
alert('true');
} else {
alert('false');
}
}
})
...
...