仅允许在 PHP 表单中选择一个单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/991025/
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-25 00:34:46  来源:igfitidea点击:

Only allowing selection of one radio button in a form with PHP

phphtmlformsradio-button

提问by cw84

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! :)

非常感谢任何帮助!:)

回答by instanceof me

Give them the same name.

给它们起相同的名字。

<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>

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

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

回答by lopamudra

Every input should have same "name"

每个输入都应该有相同的“名称”