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

查看:18
本文介绍了只允许在使用 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天全站免登陆