jQuery 比较字符串jquery运算符的正确方法=
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12527433/
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
correct way of comparing string jquery operator =
提问by Prometheus
Is this the correct way? I want the statement to run if the value of somevar equals the string?
这是正确的方法吗?如果 somevar 的值等于字符串,我希望语句运行?
if (somevar = '836e3ef9-53d4-414b-a401-6eef16ac01d6'){
$("#code").text(data.DATA[0].ID);
}
回答by Ali Habibzadeh
No. = sets somevar to have that value. use === to compare value and type which returns a boolean that you need.
不。= 将 somevar 设置为具有该值。使用 === 比较值和类型,它返回您需要的布尔值。
Never use or suggest == instead of ===. its a recipe for disaster. e.g 0 == "" is true but "" == '0' is false and many more.
永远不要使用或建议 == 而不是 ===。它是灾难的秘诀。例如 0 == "" 为真,但 "" == '0' 为假等等。
More information also in this great answer
更多信息也在这个很好的答案中
回答by sdespont
NO, when you are using only one "=" you are assigning the variable.
不,当您只使用一个“=”时,您就是在分配变量。
You must use "==" :You must use "===" :
您必须使用“==”:您必须使用“===”:
if (somevar === '836e3ef9-53d4-414b-a401-6eef16ac01d6'){
$("#code").text(data.DATA[0].ID);
}
You could use fonction like .toLowerCase()
to avoid case problem if you want
如果需要,您可以使用类似函数.toLowerCase()
来避免大小写问题
回答by bognix
First of all you should use double "==" instead of "=" to compare two values. Using "=" You assigning value to variable in this case "somevar"
首先,您应该使用双“==”而不是“=”来比较两个值。在这种情况下,使用“=”将值分配给变量“somevar”