简体   繁体   English

满足第一个条件时执行 else 块

[英]else block being executed when first condition is satisfied

I know there's a bunch of these type of questions but I can't find one that answers the underlying issue.我知道有很多这类问题,但我找不到一个能回答根本问题的问题。

I have a simple block of code where the IF condition is true but then the else block executes anyway.我有一个简单的代码块,其中 IF 条件为真,但 else 块仍然执行。 I have checked in the debug console that the condition is indeed true.我在调试控制台中检查了该条件确实为真。

    if (@Model.MajorAvailable.ToString().ToLower() == 'true') {
        $("#checkyes").attr('checked', true);
    } else {
        $("#checkno").attr('checked', true);
    }
if (@Model.MajorAvailable.ToString().ToLower() == true) {
    $("#checkyes").attr('checked', true);
} else {
    $("#checkno").attr('checked', true);
}

this code looks right but as @Bravo said still you are comparing string with boolean value.此代码看起来正确,但正如@Bravo 所说,您仍在将字符串与 boolean 值进行比较。 @Model.MajorAvailable.ToString().ToLower() this line returns stirng right? @Model.MajorAvailable.ToString().ToLower() 这一行返回搅拌对吗?

Removing the quotes from 'true' seems to have worked - which is strange as the value is no longer a boolean as ToString() has been called从 'true' 中删除引号似乎有效 - 这很奇怪,因为该值不再是 boolean 因为 ToString() 已被调用

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

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