不等于 Javascript 中要在 jQuery 中使用的符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2727957/
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
Not Equal To notation in Javascript to use in jQuery
提问by X10nD
Is this the notation to use for Not Equal To in JS, in jquery code
这是在 JS 中用于 Not Equal To 的表示法,在 jquery 代码中
!== OR !=
None of them work
他们都没有工作
Here is the code I am using
这是我正在使用的代码
var val = $('#xxx').val();
if (val!='') {
alert("jello");
}
Thanks Jean
谢谢让
回答by Deniz Dogan
Equality testing in JQuery works no different from "standard" JavaScript.
JQuery 中的相等性测试与“标准”JavaScript 没有什么不同。
!=means "not equal to", but !==makes sure that both values have the same type as well. As an example, 1 == '1'is truebut not 1 === '1', because the LHS value is a number and the RHS value is a string.
!=表示“不等于”,但!==要确保两个值也具有相同的类型。例如,1 == '1'is truebut not 1 === '1',因为 LHS 值是一个数字,而 RHS 值是一个字符串。
Given your example, we cannot really tell you a lot about what is going on. We need a real example.
鉴于你的例子,我们不能真正告诉你很多关于正在发生的事情。我们需要一个真实的例子。
回答by Francisco Soto
.val() is used to retrieve or set values from input in forms mostly, is that what you want to do? If not maybe you mean using .text() or .html().
.val() 主要用于从表单中的输入中检索或设置值,这是您想要做的吗?如果不是,也许您的意思是使用 .text() 或 .html()。
If that is indeed what you want to do maybe you have your selector wrong and its returning null to you, and null does not equal '', or maybe you actually have data there, like whitespaces. :)
如果这确实是您想要做的,也许您的选择器错误并且它向您返回 null,并且 null 不等于 '',或者您实际上在那里有数据,例如空格。:)
回答by YOU
May be you have whitespace in your #xxx node, that why both !== and != failing, you could try following to test non whitespace characters
可能您的 #xxx 节点中有空格,这就是为什么 !== 和 != 都失败的原因,您可以尝试按照以下步骤测试非空格字符
var val = $('#xxx').val();
if (/\S/.test(val)){
alert('jello');
}
Note: I assume jQuery's .val() won't return null because of this line in jQuery source
注意:我假设 jQuery 的 .val() 不会因为 jQuery 源代码中的这一行而返回 null
return (elem.value || "").replace(/\r/g, "");
If not, you need to do like this
如果没有,你需要这样做
if (val && /\S/.test(val)){
alert('jello');
}
回答by Lloyd
It's both, but the latter is strict on type, see here:
两者都是,但后者对类型有严格要求,请参见此处:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators
回答by Krunal
It is working with jquery and normal java script.
它正在使用 jquery 和普通的 java 脚本。
You should check (alert/debug) your valvariable for its value and null.
您应该检查(警报/调试)您的val变量的值和空值。
You should also check $('#xxx').lengthwhether you are getting elements or not otherwise you will get, hence your if condition will be false.
您还应该检查$('#xxx').length您是否正在获取元素,否则您将获得,因此您的 if 条件将为假。

