【问题标题】:JQuery : Radiobutton is not getting selected correctly for same control idJQuery:没有为相同的控件ID正确选择单选按钮
【发布时间】:2012-09-11 10:57:51
【问题描述】:

我正在动态创建单选按钮,并使用 Jquery 根据数据库中的值选择适当的单选按钮。下面是渲染后生成的示例代码。

<input checked="checked" id="Claim_0" name="Temp.MyClaim" type="radio" value="True" />
                <label for="Yes">Yes</label>
                <input id="Claim_0" name="Temp.MyClaim" type="radio" value="False" />
                <label for="No">No</label>

<input id="Claim_1" name="Temp.MyClaim" type="radio" value="True" />
                <label for="Yes">Yes</label>
                <input checked="checked" id="Claim_1" name="Temp.MyClaim" type="radio" value="False" />
                <label for="No">No</label>

我正在使用 JQuery 来选择值,下面是示例代码

var j = 0;
$('input:radio[id^="Claim_"]').each(function () {

    var selection = $(this)[0].defaultChecked;
    if (selection) {                        
        if ($(this).val() == "True") {
    $("input:radio[id=Claim_" + j + "]:nth(0)").attr('checked', true)
            $('#ClaimData_' + j).show();
        }
        else {
    $("input:radio[id=Claim_" + j + "]:nth(1)").attr('checked', true)
            $('#ClaimData_' + j).hide();
        }
        j++;
    }
});

问题是,在页面的最终输出中,只有最后一个单选按钮显示为选中状态,我已经动态更改了每个组的 id,但仍然只有一个单选按钮被选中。

请建议我做错了什么或者我该如何完成。

【问题讨论】:

  • 你使用 linq to sql(和 textboxfor)?
  • 所以如果值为True,应该检查,如果值不是True,应该....等待它....检查?看起来没有each() 有一种更简单的方法。
  • 即使条件selection == 'True' 为假,为什么还要使复选框为checked??
  • 即使条件选择 == 'True' 为假,我也需要将另一组单选按钮设置为假,我根据“title”属性进行区分。

标签: jquery dynamic radio-button


【解决方案1】:

您对所有单选按钮使用相同的名称Temp.MyClaim。这是一个单选按钮组,只能选择一个。

【讨论】:

    【解决方案2】:

    像这样编辑

    $('input:radio').each(function () {
    
    var selection = $(this).val();
    if (selection == 'True') {
        $(this).attr("checked", "checked");
    }
    else {
        $(this).attr("checked", "checked");        
    }
    });
    

    【讨论】:

    • i++; 怎么了?不是 each() 已经有索引了吗?
    • 我将“Title”属性更改为“title”但没有运气。我不想遍历所有单选按钮,而只想遍历那些提供“title”属性的单选按钮,否则它会对于所有单选按钮组,在 if 和 else 两种情况下都进行。
    • 然后使用 $('input:radio[title^="Claim_"]')
    【解决方案3】:
    $('input[type="radio"][title^="Claim_"]').prop('checked', true);
    

    您对所有收音机都使用相同的名称和相同的 ID,所以这会给您带来麻烦!

    【讨论】:

    • 不能在单选按钮中使用“title”属性来区分吗?我是这个属性的新手。
    • 您最好使用唯一 ID 或类别来选择电台,而不是标题。如果您对收音机使用相同的名称,它会变成一组单选按钮,在任何给定时间只能检查一个。
    • 这真的与此无关。广播组只是普通的旧 HTML。
    • 视图没有动态创建“id”“名称”“代码”@Html.RadioButtonFor(m => m.ClaimEntity.Claim, "True", Model.m.ClaimEntity.Claim.Equals( true) ? new { name = "Claim_" + ViewData["count"].ToString(), id = "Claim", title = "Claim_" + ViewData["count"].ToString() } : null) @Html .Label("Yes") @Html.RadioButtonFor(m => m.ClaimEntity.Claim, "False", Model.m.ClaimEntity.Claim.Equals(false) ? new { name = "Claim_" + ViewData["count "].ToString(), id = "Claim", title = "Claim_" + ViewData["count"].ToString() } : null) @Html.Label("No")
    猜你喜欢
    • 2012-01-08
    • 2013-06-21
    • 2014-03-31
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2016-12-21
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多