检测何时选中单选按钮

本文关键字:单选按钮 何时选 检测 | 更新日期: 2023-09-27 18:36:31

我在代码后面有一个表单构建。

我正在尝试做的是检测何时选中单选按钮,以及何时选中其更改。

我有以下

RadioButton radio = new RadioButton() { ClientIDMode = System.Web.UI.ClientIDMode.Static, AutoPostBack=true, ID = "rbOther" + testEquipment.Id, GroupName = "grp" + testEquipment.Id, Checked = false };
radio.CheckedChanged += new EventHandler(radio_CheckedChanged);

testEquipment 是存储一些数据的对象实例。

tdBrandName.Controls.Add(new TextBox() { ClientIDMode = System.Web.UI.ClientIDMode.Static, ID = "txtBrandName" + testEquipment.Id, Text = serviceStationTestEquipment != null ? serviceStationTestEquipment.BrandName : string.Empty, Width = new Unit(300), Enabled = serviceStationTestEquipment != null ? serviceStationTestEquipment.Other : false });
tdBrandName.Controls.Add(new RequiredFieldValidator() { Display = ValidatorDisplay.Dynamic, ControlToValidate = "txtBrandName" + testEquipment.Id, Text = "Required field", ErrorMessage = "Test Equipment Tab: Field is required", CssClass = "validationerror", Enabled = serviceStationTestEquipment != null ? serviceStationTestEquipment.Other : false, ID = ID = "reqValidator" + testEquipment.Id });

然后,我想在单击单选按钮时禁用文本框和必需字段验证器。有三个即。无、默认、其他

选择"其他"时,文本框必须与必填字段验证程序一起启用。

void radio_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton check = sender as RadioButton;
        if (check == null)
            return;
        string Id = check.ID.Replace("rbOther", "");
        TextBox text = check.Parent.Parent.FindControl(string.Format("txtBrandName{0}", Id)) as TextBox;
        text.Enabled = check.Checked;
        RequiredFieldValidator validator = check.Parent.Parent.FindControl(string.Format("reqValidator{0}", Id)) as RequiredFieldValidator;
        validator.Enabled = check.Checked;
    }

检测何时选中单选按钮

如果你习惯使用 jQuery,那么你可以试试这个:

<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
 $(function () {
     $("input[name$='grp']").click(function () {
         var value = $(this).val();
         id = value.replace("rbOther", "");
         $("#txtBrandName" + id).attr("disabled", "disabled");
         ValidatorEnable(document.getElementById("reqValidator" + id), false);
        //Write your code HERE
     });
 });
</script>

这是示例案例。我在 radibutton 列表中有 3 个项目,根据所选的值,我希望在标签上显示一条消息。

<asp:RadioButtonList ID="rbtnType" runat="server" RepeatDirection="Vertical">
<asp:ListItem Value="C">Contract </asp:ListItem>
<asp:ListItem Value="I">Independent </asp:ListItem>
<asp:ListItem Value="O">OutSource </asp:ListItem>
</asp:RadioButtonList>
 <br />
<asp:Label ID="lblLaborType" runat="server" ></asp:Label>
<script type="text/javascript">
 $(document).ready(function () {
    $("#<%=rbtnType.ClientID%>").change(function () {
        var rbvalue = $("input[@name=<%=rbtnType.ClientID%>]:radio:checked").val();
        if (rbvalue == "C") {           
            $('#<%=lblLaborType.ClientID %>').html('Do this');
        }
        else if (rbvalue == "I") {
            $('#<%=lblLaborType.ClientID %>').html('else this');
        }
        else if (rbvalue == "O") {
            $('#<%=lblLaborType.ClientID %>').html('or else this');
        }
    });
});  </script>