问题
I have a radwindow that I use to show error messages to users in an application.
My goal is as below;
If the message is not a warning/error I want user to be redirected when they click \"OK\" on the popped up radwindow.
To accomplish this, I\'m setting HiddenField value to \"1\" when the operation is successful and to \"0\" when the operation fails.
My problem is that when I check the HiddenField value on the client-side the IF statement always returns true, and the page is redirected.
Here are the cases when I set the hiddenfield value and set the radwindow message;
if(x)
{
hfPasswordWarning.Value = \"0\";
ShowMessage(MessageResource.ChangePasswordAuthenticateError,false);
}
else
{
hfPasswordWarning.Value = \"1\";
ShowMessage(MessageResource.ChangePasswordSuccess,true);
}
And the client-side code which I\'m having issues with, IF statement is always true;
var hv = $(\'#hfPasswordWarning\').val();
if (hv.val = \"1\") {
window.location = \"../Main/Login.aspx\";
} else {
return false;
}
I\'ve added an Alert(\"xx\") to check if it ever gets into else statement but It doesn\'t. I\'ve tried to simplify the explanation of my problem as much as I can. Thank you for your understanding.
回答1:
You need to use ==
or ===
for comparison instead of =
.
x = y
assignsy
tox
x == y
checks ify
andx
are loosely equalx === y
checks ify
andx
are equal and of the same type
So what you need to do is replacing if (hv.val = "1")
with if (hv.val == "1")
回答2:
you wrote
if (hv.val = "1")
but this is an assignment (evaluated as true, in your code): check for equality is ==
(equality by value) or ===
(equality by value and type)
回答3:
you have used = operator which is used for assignment for comparison == operator is used try this In your if statement try this
if(hv.val=="1")
来源:https://stackoverflow.com/questions/12814334/if-statement-always-true