仅允许在PHP表单中选择一个单选按钮 [英] Only allowing selection of one radio button in a form with PHP

查看:97
本文介绍了仅允许在PHP表单中选择一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常基本的问题...

A very basic question...

我如何只允许选择单选按钮列表中的一个选项?

How can I only allow the selection of one option in a list of radio buttons?

<form action="process_repair.php" method="POST">
    <label for "repair_complete">Repair complete</label>
    <input type="radio" name="Yes" value="true">
    <input type="radio" name="No" value="false">
</form>

运行此代码后,可以选择两个单选按钮,但是我希望它们可以交互,所以您只能选择一个或另一个.

When this code runs, it's possible to select both radio buttons, but I'd like them to interact so you can only select one or the other.

任何帮助,不胜感激! :)

Any help much appreciated! :)

推荐答案

给他们起相同的名字.

<form action="process_repair.php" method="POST">
 Repair complete
 <input type="radio" name="complete" value="true" id="complete_yes" />
 <label for="complete_yes">Yes</label>
 <input type="radio" name="complete" value="false" id="complete_no" />
 <label for="complete_no">No</label>
</form>

标签必须具有for属性,该属性指向相应输入的id.

Labels must have a for attribute, directing to the corresponding input's id.

这篇关于仅允许在PHP表单中选择一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆