只允许在使用 PHP 的表单中选择一个单选按钮

2022-01-21 00:00:00 radio-button forms php html

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

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.

相关文章