如何将组名应用于 asp.net 中的 HTML 单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2091574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply groupname to HTML radio buttons in asp.net?
提问by Jaqen H'ghar
Can anyone please tell about how to apply group name to html (input) radio button controls so that i can select any one of the available radio buttons?
谁能告诉我如何将组名应用于 html(输入)单选按钮控件,以便我可以选择任何一个可用的单选按钮?
I have input radios in a table. Each row contains two radios as follows. I want to select one from each row. But i am able to select only one radio button amongst all radio buttons present on all rows.
我在表格中输入了收音机。每行包含两个无线电,如下所示。我想从每一行中选择一个。但是我只能在所有行上的所有单选按钮中选择一个单选按钮。
<input name="radiobutton" type="radio" value="radiobutton" />Option1
<input name="radiobutton" type="radio" value="radiobutton" />Option2
What change i have to make to select one radio button on each row?
要在每一行选择一个单选按钮,我必须做出哪些更改?
Thanks, ~kaps
谢谢~kaps
回答by Eilon
As far as I know, radiobuttons in HTML do not have group names. Their HTML "name" attribute isthe group name.
据我所知,HTML 中的单选按钮没有组名。它们的 HTML“名称”属性是组名称。
It is important to verify that each radiobutton has a unique "value" attribute. Otherwise there is no way to tell which of the duplicate values was selected:
验证每个单选按钮是否具有唯一的“值”属性很重要。否则无法判断选择了哪个重复值:
<input name="radiobutton" type="radio" value="radiobutton1" />Option1
<input name="radiobutton" type="radio" value="radiobutton2" />Option2
回答by John K
This example lets you choose only one radio button per table row. You have to give all radio buttons the same Name= to create a mutually exclusive group of them.
此示例允许您在每个表格行中仅选择一个单选按钮。您必须为所有单选按钮提供相同的 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>
回答by Pradip Kumar Raushan
GroupName to HTML radio buttons in asp.net
组名到 asp.net 中的 HTML 单选按钮
https://www.javatpoint.com/asp-net-radiobutton
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>
After Rendering from asp.net control tag to html
从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>