开发者

Not Equal To notation in Javascript to use in jQuery

开发者 https://www.devze.com 2022-12-28 05:26 出处:网络
Is this the notation to use for Not Equal To in JS, in jquery code !== OR != None of them work Here is the code I am using

Is this the notation to use for Not Equal To in JS, in jquery code

!== OR != 

None of them work

Here is the code I am using

var val = $('#xxx').val();
if (val!='') {
 alert("jello");
}
开发者_运维问答

Thanks Jean


Equality testing in JQuery works no different from "standard" JavaScript.

!= means "not equal to", but !== makes sure that both values have the same type as well. As an example, 1 == '1' is true but not 1 === '1', because the LHS value is a number and the RHS value is a string.

Given your example, we cannot really tell you a lot about what is going on. We need a real example.


.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().

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. :)


May be you have whitespace in your #xxx node, that why both !== and != failing, you could try following to test non whitespace characters

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

return (elem.value || "").replace(/\r/g, "");

If not, you need to do like this

if (val && /\S/.test(val)){
    alert('jello');
}


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


It is working with jquery and normal java script.

You should check (alert/debug) your val variable for its value and null.

You should also check $('#xxx').length whether you are getting elements or not otherwise you will get, hence your if condition will be false.

0

精彩评论

暂无评论...
验证码 换一张
取 消