IF声明始终如一 [英] IF Statement Always True
问题描述
我有一个radwindow用于向应用程序中的用户显示错误消息。
我的目标如下:
如果消息不是警告/错误我希望用户在弹出的radwindow上单击确定时重定向。
为了实现这一点,我在操作成功时将HiddenField值设置为1,并在操作失败时将其设置为0。
我的问题是当我检查客户端的HiddenField值时,IF语句总是返回true,页面被重定向。
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.
这是我设置隐藏字段值并设置radwindow消息的情况;
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);
}
我遇到的客户端代码,IF语句总是如此;
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;
}
我添加了一个警报(xx)以检查是否曾经进入其他声明,但事实并非如此。我试图尽可能地简化我的问题的解释。感谢您的理解。
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.
推荐答案
您需要使用 ==
或 ===
用于比较而不是 =
。
You need to use ==
or ===
for comparison instead of =
.
-
x = y
将y
分配给x
-
x == y
检查y
和x
大致相等 -
x === y
检查是否y
和x
相等且类型相同
x = y
assignsy
tox
x == y
checks ify
andx
are loosely equalx === y
checks ify
andx
are equal and of the same type
所以你需要做的是用替换
if(hv.val =1)
if(hv.val = =1)
So what you need to do is replacing if (hv.val = "1")
with if (hv.val == "1")
这篇关于IF声明始终如一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!