选中时,radiogroup中的单选按钮未进行渲染=“已检查”

时间:2013-02-19 23:05:50

标签: asp.net html radio-button jquery-validation-engine

从ASP.Net MVC3(C#)应用程序获取此HTML:

<div class="row-fluid">
   <div class="span4">
      <label for="MailPrefBusi">Business Street</label>
      <input type="radio" checked="checked" class="validate[groupRequired[MailingPreference]] MailingPreference" 
             parent_tab="tab4" id="MailPrefBusi" name="MailingPreference" value="1">
   </div>
   <div class="span4">
      <label for="MailPrefBusiPO">Business PO</label>
      <input type="radio" class="validate[groupRequired[MailingPreference]] MailingPreference" 
             parent_tab="tab4" id="MailPrefBusiPO" name="MailingPreference" value="0" disabled="disabled">
   </div>
   <div class="span4">
      <label for="MailPrefHome">Home</label>
      <input type="radio" class="validate[groupRequired[MailingPreference]] MailingPreference" 
             parent_tab="tab4" id="MailPrefHome" name="MailingPreference" value="2">
   </div>
</div>

但在浏览器,FF,IE9,IE8和Chrome中查看时,MailPrefBusi收音机未显示。

以下是生成HTML的代码:

<div class="row-fluid">
    @{
        String busipref = "";
        String popref = "";
        String homepref = "";
        switch (ViewBag.part.MailingPreference)
        {
            case 1:
                busipref = "checked=\"checked\"";
                break;
            case 0:
                popref = "checked=\"checked\"";
                break;
            case 2:
                homepref = "checked=\"checked\"";
                break;
        }
     }
     <div class="span4">
        <label for="MailPrefBusi">Business Street</label>
        <input type="radio" value="1" name="MailingPreference" id="MailPrefBusi" 
               parent_tab="tab4" class="validate[groupRequired[MailingPreference]] MailingPreference" @Html.Raw(busipref) />
     </div>
     <div class="span4">
        <label for="MailPrefBusiPO">Business PO</label>
        <input type="radio" value="0" name="MailingPreference" id="MailPrefBusiPO" 
               parent_tab="tab4" class="validate[groupRequired[MailingPreference]] MailingPreference" @Html.Raw(popref) />
     </div>
     <div class="span4">
        <label for="MailPrefHome">Home</label>
        <input type="radio" value="2" name="MailingPreference" id="MailPrefHome" 
               parent_tab="tab4" class="validate[groupRequired[MailingPreference]] MailingPreference" @Html.Raw(homepref) />
     </div>
 </div>

还有这个jQuery代码:

if ($("#BusinessAddress1").val() == "") {
    $("#MailPrefBusi").attr('checked', false);
    $("#MailPrefBusi").attr('disabled', true);
} else {
    $("#MailPrefBusi").attr('disabled', false);
    if ($("#MailPrefBusi").attr('checked')) {
        $("#MailPrefBusi").attr('checked', 'checked');
    }
}
if ($("#HomeAddress1").val() == "") {
    $("#MailPrefHome").attr('checked', false);
    $("#MailPrefHome").attr('disabled', true);
} else {
    $("#MailPrefHome").attr('disabled', false);
    if ($("#MailPrefHome").attr('checked')) {
        $("#MailPrefHome").attr('checked', 'checked');
    }
}
if ($("#POAddress1").val() == "") {
    $("#MailPrefBusiPO").attr('checked', false);
    $("#MailPrefBusiPO").attr('disabled', true);
} else {
    $("#MailPrefBusiPO").attr('disabled', false);
    if ($("#MailPrefBusiPO").attr('checked')) {
        $("#MailPrefBusiPO").attr('checked', 'checked');
    }
}

但它仍未显示已检查。这是此表格的一个非常重要的部分,这个广播组必须至少检查一个项目。如果用户没有看到它被检查,我不确定该值是否被正确发送,因为我们已经有用户的MailingPreference被修改为具有空地址的值的实例,没有好处!

使用jQuery ValidationEngine进行验证。

我不确定这里到底发生了什么。我一直在高低搜索找到答案,但空出来,我尝试的一切都有相同的结果。

1 个答案:

答案 0 :(得分:1)

我很久以前也遇到过类似的问题。基本上我最终得到的是将radiobutton列表初始化放在客户端,它看起来像这样:

$(function() {
    $('input[name=MailingPreference]:eq('+@(ViewBag.part.MailingPreference)+')').attr('checked', 'checked');
});