如何将组名应用于asp.net中的HTML单选按钮?

时间:2010-01-19 06:32:36

标签: asp.net html controls

有人可以告诉我们如何将组名应用到html(输入)单选按钮控件,以便我可以选择任何一个可用的单选按钮吗?

我在表格中输入了收音机。每行包含两个无线电,如下所示。我想从每一行中选择一个。但是我能够在所有行上的所有单选按钮中只选择一个单选按钮。

<input name="radiobutton" type="radio" value="radiobutton" />Option1
<input name="radiobutton" type="radio" value="radiobutton" />Option2

每行选择一个单选按钮需要做些什么改变?

谢谢, 〜KAPS

3 个答案:

答案 0 :(得分:10)

据我所知,HTML中的radiobuttons没有组名。他们的HTML“名称”属性组名。

验证每个单选按钮是否具有唯一的“值”属性非常重要。否则,无法确定选择了哪个重复值:

<input name="radiobutton" type="radio" value="radiobutton1" />Option1 
<input name="radiobutton" type="radio" value="radiobutton2" />Option2 

答案 1 :(得分:5)

此示例允许您为每个表行选择一个单选按钮。您必须为所有单选按钮指定相同的Name =以创建它们的互斥组。

<form>

<table>
<tr><td>
    <!-- Can choose only one of these two. -->
    <input name="group1" type="radio" value="1a" />Option1 
    <input name="group1" type="radio" value="1b" />Option2 
</td></tr>
<tr><td>
    <!-- Can choose only one of these two. -->
    <input name="group2" type="radio" value="2a" />Option1 
    <input name="group2" type="radio" value="2b" />Option2 
</td></tr>
</table>

</form>

答案 2 :(得分:0)

GroupName到asp.net中的HTML单选按钮

https://www.javatpoint.com/asp-net-radiobutton

<form id="form1" runat="server">
   <table>
      <tr>
         <td>
            <asp:Label ID="Label1" runat="server" Text="Gender"></asp:Label>
         </td>
         <td>
            <asp:RadioButton ID="RadioButton1" runat="server" GroupName="gender" Text="Male" />
            <asp:RadioButton ID="RadioButton2" runat="server" GroupName="gender" Text="Female" />
         </td>
      </tr>
   </table>
</form>

从asp.net控制标签渲染到html后

<form method="post" action="./WebControlsASPX.aspx" id="form1">
   <table>
      <tr>
         <td>
            <span id="Label1">Gender</span>
         </td>
         <td>
            <input id="RadioButton1" type="radio" name="gender" value="RadioButton1" /><label for="RadioButton1">Male</label>
            <input id="RadioButton2" type="radio" name="gender" value="RadioButton2" /><label for="RadioButton2">Female</label>
         </td>
      </tr>
   </table>
</form>