【问题标题】:PHP - How do I alert if no radio button is chosen?PHP - 如果没有选择单选按钮,我如何提醒?
【发布时间】:2019-02-24 11:57:12
【问题描述】:

我有一个表单必须让用户check,但如果他们点击提交但没有选择任何单选按钮选项,我需要提醒。

 <label class="radio-inline"><input type="radio" name="optradio" value="Amount" >Amount</label>
 <label class="radio-inline"><input type="radio" name="optradio" value="Quantity" >Quantity</label>
 <label class="radio-inline"><input type="radio" name="optradio" value="Profit" >Profit</label> 

我怎样才能实现它?谢谢。

【问题讨论】:

标签: php html


【解决方案1】:

最直接的方法是使用 HTML5 验证:https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation

以下是示例:

<form>
     <label class="radio-inline"><input type="radio" name="optradio" value="Amount" required>Amount</label>
     <label class="radio-inline"><input type="radio" name="optradio" value="Quantity"  required>Quantity</label>
     <label class="radio-inline"><input type="radio" name="optradio" value="Profit" required>Profit</label> 

    <input type="submit">
</form>

注意:将required 属性添加到其中一个单选按钮也可以,但添加到每个单选按钮会更清晰。

为了更好地控制错误消息的显示方式,您必须使用 JS 编写自定义验证。

编辑: HTML5 验证不在 IE https://caniuse.com/#feat=form-validation。谢谢@JustCarty

干杯。

【讨论】:

  • 谢谢,这是一个简单的解决方案。
  • 很高兴为您提供帮助,如果此答案解决了您的问题,请单击答案旁边的复选标记将其标记为已接受。请参阅:接受答案如何工作?了解更多信息。
  • 注意,这是一个 HTML5 属性,在 IE caniuse。
猜你喜欢
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
相关资源
最近更新 更多