这里有两件事:找到控件并执行检查。在 ASP.NET 中,控件的实际 ID 和名称最终会根据它出现的 runat="server" 容器而改变,即使这些容器没有 Id。
呈现的 ASP.NET 控件始终以与您开始时相同的名称结尾,因此标记如下:
<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
可能最终被渲染为
<input type="radio" runat="server" id="ctl0$ctl0$sector1Radio" name="ctl0_ctl0_SectorGroup" title="Sector1" />
如果您在 JQuery 中使用“包含”选择语法,即使在呈现它之后,您也可以找到该元素。所以要找到这个元素,一旦渲染,你可以使用:
$("input[type='radio'][id*='$sector1Radio']")
此语法将查找 id 包含“$sector1Radio”的任何单选按钮
拥有该元素后,您可以使用以下代码选中或取消选中它,您可以从其他元素的 click 事件中调用该代码。
// check the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', true);
// uncheck the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', false);
最后一件事...如果您只想在按下按钮时单击一个文本块(将其包装在标签中并将 AssociatedControlId 属性设置为单选按钮的控件名称,如下所示...
<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
<asp:label runat="server" id="lblsector1Radio" associatedControlID="sector1Radio">clicking here clicks and unclicks the radio button</asp:label>