Html 如何识别接受哪个单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3176096/
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 identify which radio button is accepted?
提问by sark9012
How can I identify which radio button is accepted?
如何确定接受哪个单选按钮?
How do I prevent a user being able to select multiple options when I use radio buttons?
当我使用单选按钮时,如何防止用户能够选择多个选项?
<input type='radio' name='one'>option1<br />
<input type='radio' name='two'>option2<br />
<input type='radio' name='three'>option3<br />
<input type='radio' name='four'>option4<br />
<input type='radio' name='five'>option5<br />
Thats the code I use, thanks for reading.
这就是我使用的代码,感谢阅读。
回答by superUntitled
You need to use the "name" attribute to tie them all together, use the value attribute to give them different values.
您需要使用“名称”属性将它们联系在一起,使用 value 属性为它们提供不同的值。
<input type="radio" name="number" value="one" /> One<br />
<input type="radio" name="number" value="two" /> Two<br />
<input type="radio" name="number" value="three" /> Three<br />
Think of name as last name... For example, if you had Bart, Lisa, Homer, Marge and Maggie, the name would be their last name: Simpson, the value would be the said names:
将 name 视为姓氏...例如,如果您有 Bart、Lisa、Homer、Marge 和 Maggie,则名称将是他们的姓氏:Simpson,值将是上述姓名:
<input type="radio" name="Simpson" value="Bart" /> Bart<br />
<input type="radio" name="Simpson" value="Lisa" /> Lisa<br />
<input type="radio" name="Simpson" value="Marge" /> Marge<br />
<input type="radio" name="Simpson" value="Homer" /> Homer<br />
<input type="radio" name="Simpson" value="Maggie" /> Maggie<br />
回答by Sergey Eremin
you should give them the same name
你应该给他们同一个名字
回答by user1131926
Also the order of name/value pair matter's I guess.Name should appear before the order
我猜测名称/值对的顺序也很重要。名称应该出现在顺序之前
回答by user1131926
I know! Do like this:
我知道!这样做:
<label><input type="radio" name="a">1</label>
<label><input type="radio" name="a">2</label>
<label><input type="radio" name="a">3</label>
Give everything same value for 'name' attribute. You add the name for the radio buttons
为“名称”属性赋予所有相同的值。您为单选按钮添加名称