简体   繁体   English

Javascript将布尔值与True进行比较

[英]Javascript comparing boolean value to True

I am trying to compare a database (SQL) value (which is being returned correctly) to the boolean value 'true'. 我试图将数据库(SQL)值(正确返回)与布尔值'true'进行比较。 If the database bit value is = true then I want a div element to become visible, else stay hidden. 如果数据库位值为= true,那么我希望div元素变为可见,否则保持隐藏状态。

<script language="javascript">
    window.onload= function show_CTL() {
        if(<%=_CurrentUser.IsCTL%> == true){
            document.getElementById('CTL').style.visibility = "visible";
        } else{
            document.getElementById('CTL').style.visibility = "hidden";
        }
    }    
</script>

However I am getting the error, Javascript: 'True' is undefined. 但是我收到错误,Javascript:'True'未定义。

I have tried many combinations of <%=_CurrentUser.IsCTL%> == 'true' or "true" or true or "True" or 'true' and even the === ... but all give me the same error message. 我尝试了很多<%= _ CurrentUser.IsCTL%> =='true'或“true”或者true或“True”或“true”的组合,甚至是=== ......但都给了我相同的错误信息。

Any insights on how to resolve this issue will be greatly appreciated. 任何有关如何解决此问题的见解将不胜感激。

I have such comparisons successfully before with integer values such as:- 我之前使用整数值成功进行了这样的比较,例如: -

 window.onload= function show() {
    if(<%=_CurrentUser.RoleKey%> == 1 || <%=_CurrentUser.RoleKey%> == 2)
            document.getElementById('enr').style.visibility = "visible";
    else
            document.getElementById('enr').style.visibility = "hidden";
 }

Do this: 做这个:

if("<%=_CurrentUser.IsCTL%>" === "True")

<%=_CurrentUser.IsCTL%> is returning True . <%=_CurrentUser.IsCTL%>返回True So wrap it with string and compare them instead. 所以用字符串包装并比较它们。 Notice the '===' instead of '=='. 请注意'==='而不是'=='。

In

if(<%=_CurrentUser.IsCTL%> == true)

I think <%=_CurrentUser.IsCTL%> is getting evaluated to True before the code is seen by the browser. 我认为<%=_CurrentUser.IsCTL%>在浏览器看到代码之前被评估为True。
The browser will see this as 浏览器会将此视为

if(True == true)

True does not make a lot of sense to the browser, thats why the error. True对浏览器没有多大意义,这就是为什么错误。 For this true to be treated as a boolean, try one of this: 要将此true视为布尔值,请尝试以下方法之一:

if(new Boolean('<%=_CurrentUser.IsCTL%>') == true)

or 要么

if(new Boolean('<%=_CurrentUser.IsCTL%>'))

This has gotten me before as well. 这也让我受益匪浅。 ASP.NET will return True for a boolean which is true. ASP.NET将为布尔值返回True ,该值为true。 You have to make it a string and then compare it to the string version == "True" in order to get a proper conditional statement. 你必须使它成为一个字符串,然后将它与字符串版本== "True"进行比较,以获得适当的条件语句。

Conversely, you could also just make a variable in javascript 相反,你也可以在javascript中创建一个变量

var True = true;

You need to convert your native boolean value to the string "true" before output. 您需要在输出之前将本机布尔值转换为字符串“true”。 So, assuming ASP.NET MVC, I believe it looks like: 所以,假设ASP.NET MVC,我相信它看起来像:

<%=_CurrentUser.IsCTL ? "true" : "false"%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM