IF Statement Always True

自作多情 提交于 2019-11-26 06:08:47

问题


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 assigns y to x
  • x == y checks if y and x are loosely equal
  • x === y checks if y and x 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!