简体   繁体   English

如何在没有字段集的情况下对表单输入进行分组?

[英]How to group form inputs accessibly without a fieldset?

Problem: Grouping form elements 问题:分组表单元素

I have an HTML form, where in few places a single control is composed of several inputs. 我有一个HTML表单,在很少的地方,一个控件由几个输入组成。 One example is a group of radio buttons. 一个例子是一组单选按钮。

I'd like to group those inputs and it's label explicitly, so that the screen readers would also (in addition to the visual representation by aligning them on a single line) be able to understand and announce this relationship. 我想明确地对这些输入及其标签进行分组,以便屏幕阅读器(除了通过将它们对齐在一条线上的可视化表示之外)能够理解并宣布这种关系。

For example, let's say I have a control like this: 例如,假设我有这样的控件:

<div class="control">
  <div class="control-label">Type</div>
  <div class="control-inputs">
    <input type="radio"
           name="type"
           value="a"
           id="type-a" />
    <label for="type-a">A</label>

    <input type="radio"
           name="type"
           value="b"
           id="type-b" />
    <label for="type-b">B</label>
  </div>
</div>

Standard solution problems: Fieldset styling issues 标准解决方案问题:Fieldset样式问题

fieldset element and it's child legend seem to be made exactly for that (used in the example below). fieldset元素和它的子legend似乎完全是为了那个(在下面的例子中使用)。

The problem is that fieldset and legend elements can't be styled like normal elements ( some discussion about it ) and nowadays other than in Firefox it's impossible to align them on a single line using Flexbox, which my layout requires. 问题是fieldsetlegend元素不能像普通元素一样( 关于它的一些讨论 ),而现在除了在Firefox之外,使用我的布局所需的Flexbox将它们对齐在一条线上是不可能的。

<fieldset class="control">
  <legend class="control-label">Type</legend>
  <div class="form-inputs">
    <input type="radio"
           name="type"
           value="a"
           id="type-a" />
    <label for="type-a">A</label>

    <input type="radio"
           name="type"
           value="b"
           id="type-b" />
    <label for="type-b">B</label>
  </div>
</fieldset>

Question: Is there some other way? 问:还有其他方法吗?

That makes me wonder if there is some accessible way to group several form controls other than using fieldset element? 这让我想知道除了使用fieldset元素之外是否有一些可访问的方法来分组几个表单控件?

Possible solution: role="group" ? 可能的解决方案: role="group"

There is a "group" role (used in the example below), which could be added to a simple div and it looks like it might do the job, but nowhere is stated clearly that it is the functional equivalent to using a fieldset. 有一个“组”角色 (在下面的例子中使用),可以添加到一个简单的div ,看起来它可能会完成这项工作,但是没有明确说明它与使用字段集的功能相同。 And if it does, then how do I mark an element of this group to serve as an equivalent of legend ? 如果确实如此,那么我如何标记该组的元素作为legend的等同物呢?

<div role="group"
     class="control">
  <div class="control-label">Type</div>
  <div class="control-inputs">
    <input type="radio"
           name="type"
           value="a"
           id="type-a" />
    <label for="type-a">A</label>

    <input type="radio"
           name="type"
           value="b"
           id="type-b" />
    <label for="type-b">B</label>
  </div>
</div>

Basically you have already answered your question in the Possible Solution section (btw, as a blind person, I'm just impressed how you styled your question with headings!). 基本上你已经在可能的解决方案部分回答了你的问题(顺便说一下,作为一个盲人,我只是对你用标题设置问题的方式印象深刻!)。 You missed one tiny and simple thing, the aria-label attribute: 你错过了一个小而简单的东西, aria-label属性:

<div role="group" class="control" aria-label="Type">

Note: this will be invisible on screen, it is a screen-reader only solution. 注意:这在屏幕上是不可见的,它只是一个屏幕阅读器解决方案。 If however you want to make it visible, do the following using the aria-labelledby attribute instead: 但是,如果要使其可见,请使用aria-labelledby属性执行以下操作:

<div role="group" class="control" aria-labelledby="pseudolegend">
<div id="pseudolegend" class="style-it-like-a-legend">Type</div>
[...]
</div>

The pseudolegend may be a span or even a p , of course, if you find it more appropriate. 当然, pseudolegend可能是span甚至是p ,如果你觉得它更合适。
A quick and dirty local test I made showed that, at least with JAWS and Chrome, there is no difference between a fieldset and a div with aria-label . 我做的快速而肮脏的本地测试表明,至少在JAWS和Chrome中, fieldset和带有aria-labeldiv之间没有区别。

Note: For radio button groups in particular you can use role=radiogroup . 注意:对于单选按钮组,您可以使用role=radiogroup Also, in order for the semantics of a group or radiogroup to be expressed to screen reader users an accessible name for the grouping element is required. 而且,为了将groupradiogroup group的语义表达给屏幕阅读器用户,需要用于分组元素的可访问名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM