【问题标题】:only one radiobutton selection in repeater中继器中只有一个单选按钮选择
【发布时间】:2012-06-18 13:59:51
【问题描述】:

我只想选择一个单选按钮,但我不能这样做。 这是我的代码:

<asp:Repeater ID="Repeater_secenekler" runat="server" DataSource='<%#Eval("Secenekler")%>'>
   <HeaderTemplate>
       <table>
   </HeaderTemplate>
   <ItemTemplate>
       <tr>
           <td style="margin-left: 20px;">
               <asp:CheckBox ID="CheckBox" runat="server" Text='<%#Eval("OptionName")%>' Visible='<%# Eval("TypeId").ToString() == "1" %>' />
               <asp:RadioButton ID="RadioButton" runat="server" Text='<%#Eval("OptionName")%>' Visible='<%# Eval("TypeId").ToString() == "2" %>'
                    GroupName="RadioButtonGrup" />
           </td>
       </tr>
   </ItemTemplate>
   <FooterTemplate>
       </table>
   </FooterTemplate>
</asp:Repeater>

我可以选择所有具有相同组名的单选按钮,但我不想要这个。我只想要一个选项。

我该怎么做。 谢谢。

【问题讨论】:

    标签: asp.net radio-button repeater


    【解决方案1】:

    这是使用 RadioButtons 的 ASP.NET 中继器的一个众所周知的错误:

    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316495

    这里有一个修复:

    http://www.ifinity.com.au/Blog/EntryId/87/Simple-fix-for-Radio-Button-controls-in-an-ASP-NET-Repeater-using-jQuery

    jQuery("[name$='$optValue']").attr("name",jQuery("[name$='$optValue']").attr("name"));
    
    jQuery("[name$='$optValue]").click(function (){ 
                    //set name for all to name of clicked 
                    jQuery("[name$='$optValue]").attr("name", this.attr("name")); 
                });
    

    【讨论】:

    • 太棒了...有一个甜甜圈;-)
    【解决方案2】:

    只是对上述答案的快速修复,因此在执行脚本时不会出现任何错误。

        $(function () {
        $('[name$="$YourGroupName"]').attr("name", $('[name$="$YourGroupName"]').attr("name"));
    
        $('[name$="$YourGroupName"]').click(function () {
            //set name for all to name of clicked 
            $('[name$="$YourGroupName"]').attr("name", $(this).attr("name"));
        });
    });
    

    【讨论】:

    • 事实上,在您 (@xfan) 进行此修复之前,以前的解决方案对我不起作用。谢谢!!
    • 谢谢!这个答案立即解决了我的问题。
    • 为什么不将所有单选按钮的名称属性设置为相同的值并保持不变?
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2011-08-13
    • 2012-04-23
    • 2018-10-27
    • 2012-12-28
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多