0

我刚刚看到了RadioButtonForwithtruefalseas 2nd 参数的实现,但是如何使用 JavaScript 设置检查值?

我已经有一个现有代码,它通过 Id in 设置单选按钮的选中值,JavaScript并且仅@Html.RadioButton在 View 中使用。但我现在想要的是使用RadioButtonFor. 我该怎么做呢?我的 active 和 inactive 具有相同的 Id,两者 rbtn 仅值不同。

PS:不工作的代码在 JavaScript 的底部

模型:

[Display(Name = "Status")]
public string userStatus { get; set; }

看法:

@Html.LabelFor(m => m.userStatus)
@Html.LabelFor(m => m.userStatus, "Active")
@Html.RadioButtonFor(m => m.userStatus, true)
@Html.LabelFor(m => m.userStatus, "Inactive")
@Html.RadioButtonFor(m => m.userStatus, false)

Javascript的一部分:

//columnData - represents the column index in my table
//fieldId - id of the fields (e.g. text = userDesc, radio = userStatus)

for (i = 0; i < columnData.length; i++) {
    //Evaluates what type an input is
    var elmntType = document.getElementById(fieldId[i]).getAttribute("type");

    if (elmntType == "text") {

        document.getElementById(fieldId[i]).value = rowSelected.cells[i].innerHTML.trim();

    } else if (elmntType == "radio") {

        var status = rowSelected.cells[i].innerHTML.trim();

        //I just invented this but it's not working
        if (status == "Active") {
            document.getElementById(fieldId[i]).checked = true;
        } else {
            document.getElementById(fieldId[i]).checked = false;
        }
    }               
}
4

0 回答 0