【问题标题】:Radio button in radiogroup not rendering checked when checked="checked"选中 =“已选中”时未选中单选按钮组中的单选按钮
【发布时间】:2013-02-19 23:05:50
【问题描述】:

从 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 进行验证。

我不确定这里到底发生了什么。我到处搜索以找到答案,但结果都是空的,我尝试的所有结果都是相同的。

【问题讨论】:

  • 使用 jQuery .prop() 函数代替 .attr():api.jquery.com/prop
  • 仍然认为这不会有所作为。
  • @Hanlet Escañ0 - 改为使用 .prop() 并且它有效。谢谢。

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


【解决方案1】:

不久前我遇到了类似的问题。基本上我最终的结果是将单选按钮列表初始化放在客户端,它看起来像这样:

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

【讨论】:

  • 我接受了这个,但它没有选择正确的收音机,VALUE = ViewBag.part.MailingPreference;它只选择组的第一个(索引)。
  • 这确实有效:$('input[name=MailingPreference]').filter(function(){return $(this).val() == @(ViewBag.part.MailingPreference); })
  • 添加此代码以根据 ViewBag 值选择性别:$('select[name=Gender] option').filter(function(){return $(this).val() == @(ViewBag.part.Gender); }).prop('selected', true); 这个 filter() 函数很酷。
猜你喜欢
  • 2019-10-04
  • 1970-01-01
  • 2017-07-24
  • 2023-04-09
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
相关资源
最近更新 更多