是否应该始终选中单选组中的一个 HTML 单选按钮?

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

是否有任何标准(HTML、UI、可访问性等)规定应始终选择给定单选按钮组中的一个单选按钮?

Are there any standards (HTML, UI, accessibility, and such like) that stipulate that one of the radio buttons in a given group of radio buttons should be selected at all times?

我遇到了一个业务需求,我被要求不选中组中的两个单选按钮,然后让逻辑强制用户在继续之前选择一个.

I have encountered a business requirement whereby I have been asked that both radio buttons in a group be left unchecked, and then to have logic forcing the user to select one before they can continue.

虽然我知道如何实现这一点,但感觉不对,因此我暗示了这一点,但我一直在寻找更明确地规定这一点的指南,以便我可以将其纳入我们自己的标准.

While I know how to achieve this, it felt wrong, and I intimated as such, but was looking for guidelines that stipulate this more explicitly so I can feed this into our own standards.

推荐答案

是:http://www.w3.org/TR/html401/interact/forms.html#radio

始终只选中一组中的一个单选按钮.如果一组单选按钮的任何元素都没有指定已检查",则用户代理必须首先检查该组的第一个单选按钮.

At all times, exactly one of the radio buttons in a set is checked. If none of the elements of a set of radio buttons specifies `CHECKED', then the user agent must check the first radio button of the set initially.

规范使用术语用户代理"来表示通常称为浏览器".所以规范说如果没有检查,浏览器会先检查.

The specification uses the term "user agent" for what is commonly known as "browser". So the specification says that if none is checked, the browser will check the first.

更新: 请注意,我尝试过的 4 个浏览器都没有真正做到这一点! 他们不检查第一个并且 没有 收音机是以 =on 形式提供给服务器.一个好的 Web 框架应该进行服务器端检查(无论如何都应该这样做,因为突然或伪造的 POST 可能会导致相同的结果).

UPDATE: note that none of the 4 Browsers i tried actually does this! They dont check the first and none of the radios are given as =on to the server. A good web framework should do the serverside-checking (it should do it anyway because an abrupted or forged POST could cause the same).

相关文章