javascript - 比较 JavaScript 中的字符串

标签 javascript string compare

在 jquery ajax 函数中,我接收到服务器“true”或“false”字符串。然后我只知道结果,但只在 if 条件下得到 false。

 $.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');
            }
         }
    })
...

...

有人可以帮助我吗?

最佳答案

“结果”中可能有换行符或多余空格。

关于javascript - 比较 JavaScript 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10411150/

相关文章:

c# - C#比较两个字符串

Python 在嵌套列表中搜索以查找值并将值插入到新列表中

javascript - 将查询结果推送到对象中

javascript - 我有一个 JQuery 可以工作,但有一点问题

javascript - 在菜单中嵌入 YUI Treeview

string - String Literal Slice 与 String 引用一起使用是否有效?

c++ - 设置<字符串> : how to list not strings starting with given string and ending with `/` ?

java - 如何在回收 View 中隐藏部分字符串?

python - 哪些词典项目可以放入其他词典中

javascript - 删除 <select> 列表中的选项