繁体   English   中英

单选按钮呈现为“ checked = checked”,不确定来源

[英]Radio buttons are rendering as 'checked=checked', not sure of source

我的页面上有8个不同的单选按钮组(“是/否”按钮组)。 最初,我已将html编码为在选中“否”按钮的情况下进行渲染,但是现在我需要在未选择按钮的情况下进行渲染。

我删除了“已检查=已检查”并刷新,但仍选择了“否”按钮。 我再次刷新,清除了缓存,并在其他浏览器上运行无济于事。 我正在寻找可能的原因的协助。

我的MVC代码:

@Html.RadioButtonFor(m => m.IsFelon, true, new { @class = "commentBox RadioIf" })
    <label for="PersonModel_Felon">Yes</label>
@Html.RadioButtonFor(m => m.IsFelon, false, new { @class = "commentBox" })
    <label for="PersonModel_Felon">No</label>

呈现方式:

<input class="commentBox" data-val="true" data-val-required="The ChildAbusePast field is required." id="ChildAbusePast" name="ChildAbusePast" type="radio" value="True" />
   <label for="HistoryModel_ChildAbusePast">Yes</Label>
<input checked="checked" class="commentBox" id="ChildAbusePast" name="ChildAbusePast" type="radio" value="False" />
   <label for="HistoryModel_ChildAbusePast">No</Label>

唯一涉及到此的是一些jquery:

$(document).ready(function () {
        $("input.commentBox").click(function () {
            if ($(this).closest('div.editor-field2').children('input:checked').val() == "True") {
               $(this).closest('div.formQuestion').children('div.hidden').slideDown(800);
          } else {
               $(this).closest('div.formQuestion').children('div.hidden').slideUp("fast");
         }
        });
    });

如果未初始化IsFelon,则默认情况下会将其设置为“ False”,这是对象的布尔类型的性质。

另一方面,如果您希望默认情况下取消选中广播,那么您不应该使用bool,而是要使用“ bool?”。 它将默认值设置为“ null”,因此将不会选择任何无线电。

您模型的IsFelon属性是否有值? 如果是标准布尔值,它将。 因此,当您将该属性映射到值为False的单选按钮时,MVC会识别出它们匹配,并将按钮设置为checked

我能想到的最简单的解决方案是不将那些单选按钮映射到模型属性,而只是检索这些值并在回发时在控制器中以编程方式设置模型属性。 您还可以考虑使IsFelon成为可为空的属性,但是在MVC中使用可为空的属性可能会涉及一些麻烦。

暂无
暂无

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

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