簡體   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