javascript - 如何使用javascript/jquery检查asp:repeater中的单选按钮是否被选中
问题描述
我在 asp:repeater 中定义了两个单选按钮(“是”、“否”)。这在运行时是动态的,这意味着如果数据库返回 5 个问题,那么对于每个问题我们都有选项('Yes','No')。所附图片显示了单选按钮的现有设计
现在我想验证在 asp:repeate 中定义的这些单选按钮,以检查单选按钮是否被选中或不使用 javascript/jquery。下面是代码:
<asp:Repeater ID="rpSuppQuestions" runat="server" OnItemCommand="rpSuppQuestions_ItemCommand" OnItemDataBound="rpSuppQuestions_ItemDataBound">
<ItemTemplate>
<div class="div_soru_wrapper">
<table width="100%" border="0" style="text-align: left">
<tr>
<td align="left" style="vertical-align: top;">
<asp:HiddenField ID="hdnQuestionID" Value='<%# Bind("Question_ID") %>' runat="server" />
<asp:Label ID="Label1" runat="server" Text="Q" CssClass="labelSmallBold"><%# Convert.ToString(Container.ItemIndex + 1) %>:</asp:Label>
</td>
<td align="left">
<asp:Label ID="lblSuppQuestions" runat="server" Text='<%# Bind("Question_description") %>'
CssClass="labelSmallBold" Width="900px"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label5" runat="server" Text="A" CssClass="labelSmallBold"><%# Convert.ToString(Container.ItemIndex + 1) %>:</asp:Label>
</td>
<td align="left">
<table align="left">
<tr>
<td>
<asp:RadioButton ID="rbYes" runat="server" type="radio" CssClass="rpyes" name="tttt" GroupName="rbAnswer" Text="Yes" Font-Names="Verdane" Font-Size="10" ForeColor=" #936E85" Visible="true"/>
<asp:RadioButton ID="rbNo" runat="server" CssClass="rpyes" GroupName="rbAnswer" Text="No" Font-Names="Verdane" Font-Size="10" ForeColor=" #936E85" Visible="true"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
解决方案
我认为您正在寻找的是使用 jQuery 选择 asp.net 控件的方法。我建议ClientIDMode="Static"
在控件上使用 .NET Framework 4.0 中引入的 ,以使其 ID 保持不变,您的 html 应如下所示:
<asp:RadioButton ClientIDMode="Static" ID="rbYes" runat="server" type="radio" CssClass="rpyes" name="tttt" GroupName="rbAnswer" Text="Yes" Font-Names="Verdane" Font-Size="10" ForeColor=" #936E85" Visible="true"/>
<asp:RadioButton ClientIDMode="Static" ID="rbNo" runat="server" CssClass="rpyes" GroupName="rbAnswer" Text="No" Font-Names="Verdane" Font-Size="10" ForeColor=" #936E85" Visible="true"/>
然后你可以在 javascript/jQuery 中使用普通的 ID 选择器,它看起来像这样:
if($("rbYes").prop("checked", true)) {
//Your code here
}