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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:11:30  来源:igfitidea点击:

How to require radio button selection in html5 form

htmlformsvalidationradio-buttonform-submit

提问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>&nbsp;
        <span>Mister</span>
      </span>
      <span>
        <input name="staatus" type="radio" value="Missis">&nbsp;
        <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 requiredattribute.

使用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>&nbsp;
        <span>Mister</span>
      </span>
      <span>
        <input name="staatus" type="radio" value="Missis">&nbsp;
        <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">&nbsp;
    <span>Mister</span>
  </span>
  <span>
    <input name="status" type="radio" value="Missis">&nbsp;
    <span>Missis</span>
  </span>