今天在编写代码时遇到Repeater控件中的Radiobutton(单选按钮)不能单选,是因为Repeater生成的Radiobutton
的Id是动态生成的,无法单选。我在网上找到一个解决办法,大家有遇到相同问题的,可以来看看。
<script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript"> function selectSingleRadio(rbtn1, GroupName) { $("input[type=radio]").each(function (i) { if (this.name.substring(this.name.length - GroupName.length) == GroupName) { this.checked = false; } }) rbtn1.checked = true; } </script>
<asp:Repeater ID="r_economicList" runat="server"> <ItemTemplate> <input type="radio" id="economic" name="economic" value='<%# Eval("Id") %>' runat="server" οnclick="return selectSingleRadio(this,'economic');" /> <%#Eval("EconomicName") %> </ItemTemplate> </asp:Repeater>
string economicId; foreach (RepeaterItem item in r_economicList.Items) { HtmlInputRadioButton economic = (HtmlInputRadioButton)item.FindControl("economic"); if (economic.Checked) { economicId = economic.Value.ToString(); } }
economicId就是我要的值