Html 如何要求在 html5 表单中选择单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22281016/
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 require radio button selection in html5 form
提问by Andrus
html 5 form contains two radio buttons. How to force user to select one radion button before submitting form ? I tried to use required but there is not radio button group, required shoult applited to group.
html 5 表单包含两个单选按钮。如何强制用户在提交表单之前选择一个单选按钮?我尝试使用 required 但没有单选按钮组,需要应用于组。
<form action="/Home/Sendtitle" method="post">
<p>
Title* <span>
<span>
<span>
<input name="staatus" type="radio" value="Mister" autofocus>
<span>Mister</span>
</span>
<span>
<input name="staatus" type="radio" value="Missis">
<span>Missis</span>
</span>
</span>
</span>
</p>
<p>
<input type="submit" value="Send title" >
</p>
</form>
采纳答案by arivu86
You can use the property required as its a boolean attribute that solve ur problem. required="required"
您可以使用所需的属性作为解决您的问题的布尔属性。必需=“必需”
回答by Arjit
Use required
attribute.
使用required
属性。
<input name="staatus" type="radio" value="Missis" required>
回答by Atif Tariq
You just need to set the required-attribute for one input of the radiogroup, but you can set it for all.
您只需要为 radiogroup 的一个输入设置 required-attribute,但您可以为所有输入设置它。
<form action="/Home/Sendtitle" method="post">
<p>
Title* <span>
<span>
<span>
<input name="staatus" type="radio" value="Mister" autofocus required>
<span>Mister</span>
</span>
<span>
<input name="staatus" type="radio" value="Missis">
<span>Missis</span>
</span>
</span>
</span>
</p>
<p>
<input type="submit" value="Send title" >
</p>
</form>
回答by Dionny Prensa
And you can use it once:
您可以使用一次:
<span>
<input name="status" type="radio" value="Mister" autofocus required="required">
<span>Mister</span>
</span>
<span>
<input name="status" type="radio" value="Missis">
<span>Missis</span>
</span>