I was writing a very simple function but can't get the if statement to work correctly.
$("#supplyRequestTextbox-clients").live("blur", function() {
var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");
if (textboxValue == "NO ACCOUNT") {
$(".controlBarTextboxNoAccount").fadeIn("fast");
}
});
The value in the input #supplyRequestTextbox-clients represents a client and is organized like this:
id# FirstName LastName email phone An example: 000001 John Doe johndoe@johndoe.com 123-456-7890
When no account is found, the string appears exactly like this: 000906 NO ACCOUNT
In my function I strip the digits and first space, then check to see if it works in my if statement. I have alerted textboxValue and it is passing correctly, but no matter that textboxValue is, the if statement never fires.
Change the if condition to:
if(textboxValue.indexOf("NO ACCOUNT") !== -1)
indexOf("NO ACCOUNT")
finds "NO ACCOUNT"
within textboxValue
, if it can't find it -1
is returned. So this will be true if "NO ACCOUNT"
is anywhere in your string.
Use ==
for comparisons. You are assigning a truthy value, so the statement always fires.
Change your if
condition to
if (textboxValue == "NO ACCOUNT")
If you do
if (textboxValue = "NO ACCOUNT")
You're actually assigning "NO ACCOUNT"
to textboxValue
and evaluating the result as bool.
Try doing this:
$("#supplyRequestTextbox-clients").live("blur", function() {
var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");
// check what textboxValue evaluates to:
alert(textboxValue);
if (textboxValue == "NO ACCOUNT") {
$(".controlBarTextboxNoAccount").fadeIn("fast");
}
});
Use ==
for comparisons as Kolink suggest.
You are replacing " "
with ""
, IE. removing all spaces so your if statement will never return true.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.