Html 一种形式的多个单选按钮组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28543752/
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
Multiple radio button groups in one form
提问by AlexG
Is it possible to have multiple radio button groups in a single form? Usually selecting one button deselects the previous, I just need to have one of a group deselected.
是否可以在一个表单中包含多个单选按钮组?通常选择一个按钮会取消选择前一个按钮,我只需要取消选择一组按钮。
<form>
<fieldset id="group1">
<input type="radio" value="">
<input type="radio" value="">
</fieldset>
<fieldset id="group2">
<input type="radio" value="">
<input type="radio" value="">
<input type="radio" value="">
</fieldset>
</form>
回答by pankijs
Set equal name
attributes to create a group;
设置相等的name
属性来创建一个组;
<form>
<fieldset id="group1">
<input type="radio" value="value1" name="group1">
<input type="radio" value="value2" name="group1">
</fieldset>
<fieldset id="group2">
<input type="radio" value="value1" name="group2">
<input type="radio" value="value2" name="group2">
<input type="radio" value="value3" name="group2">
</fieldset>
</form>
回答by Kunvar Singh
Just do one thing, We need to set the name property for the same types. for eg.
只做一件事,我们需要为相同类型设置 name 属性。例如。
Try below:
试试下面:
<form>
<div id="group1">
<input type="radio" value="val1" name="group1">
<input type="radio" value="val2" name="group1">
</div>
</form>
And also we can do it in angular1,angular 2 or in jquery also.
而且我们也可以在 angular1、angular 2 或 jquery 中做到这一点。
<div *ngFor="let option of question.options; index as j">
<input type="radio" name="option{{j}}" value="option{{j}}" (click)="checkAnswer(j+1)">{{option}}
</div>
回答by Nahid Rehman
This is very simple you need to keep different names of every radio input group.
这很简单,您需要保留每个收音机输入组的不同名称。
<input type="radio" name="price">Thousand<br>
<input type="radio" name="price">Lakh<br>
<input type="radio" name="price">Crore
</br><hr>
<input type="radio" name="gender">Male<br>
<input type="radio" name="gender">Female<br>
<input type="radio" name="gender">Other
回答by Mosfeq Anik
in input field make name same like
在输入字段中使名称相同
<input type="radio" name="option" value="option1">
<input type="radio" name="option" value="option2" >
<input type="radio" name="option" value="option3" >
<input type="radio" name="option" value="option3" >
回答by user3844710
To create a group of inputs you can create a custom html element
要创建一组输入,您可以创建自定义 html 元素
window.customElements.define('radio-group', RadioGroup);
https://gist.github.com/robdodson/85deb2f821f9beb2ed1ce049f6a6ed47
https://gist.github.com/robdodson/85deb2f821f9beb2ed1ce049f6a6ed47
to keep selected option in each group, you need to add name attribute to inputs in group, if you not add it then all is one group.
要在每个组中保留选定的选项,您需要将 name 属性添加到组中的输入,如果不添加它,则全部为一组。