简体   繁体   English

可访问性和复杂性 HTML forms:如何在表单控件之间使用标题?

[英]Accessibility and complex HTML forms: How to use headings between form controls?

A client needs to create quite a complex form.客户需要创建一个相当复杂的表单。 We use <fieldset> / <legend> to group certain elements together.我们使用<fieldset> / <legend>将某些元素组合在一起。 But it's not enough - even if we nest them.但这还不够——即使我们嵌套它们。

Think about the following:考虑以下几点:

<form>
  <fieldset>
    <legend>Your personal details</legend>

    <label>First name: <input name="first_name"></label>
    <label>Last name:  <input name="last_name"></label>

    <fieldset>
      <legend>Gender</legend>

      <label>Male:   <input type="radio" name="gender" value="male"></label>
      <label>Female: <input type="radio" name="gender" value="female"></label>
      <label>Other:  <input type="radio" name="gender" value="other"></label>
    </fieldset>
  </fieldset>
</form>

Imagine this is only a small part of a bigger form.想象一下,这只是更大形式的一小部分。 For example, we could have a "postal address" and a "billing address".例如,我们可以有一个“邮政地址”和一个“帐单地址”。 Would we need to "indent" the whole thing once more using <fieldset> / <legend> s?我们是否需要使用<fieldset> / <legend>再次“缩进”整个内容?

<form>
  <fieldset>
    <legend>Postal address</legend>

    <fieldset>
      <legend>Your personal details</legend>
      ...

      <fieldset>
        <legend>Gender</legend>
        ...
      </fieldset>
    </fieldset>
  </fieldset>

  <fieldset>
    <legend>Billing address</legend>

    ...
  </fieldset>
</form>

Or should we rather use headings?还是我们应该使用标题?

<form>
  <h2>Postal address</h2>

  <fieldset>
    <legend>Your personal details</legend>
    ...

    <fieldset>
      <legend>Gender</legend>
      ...
    </fieldset>
  </fieldset>

  <h2>Billing address</h2>
  ...
</form>

While it's probably valid to nest <fieldset> / <legend> s, I would not over-do it.虽然嵌套<fieldset> / <legend>可能是有效的,但我不会过度这样做。 Party because JAWS will announce the <legend> for each contained input element (in contrast to NVDA that will only announce it once, when reaching the first contained input element).参与,因为 JAWS 将为每个包含的输入元素宣布<legend> (与 NVDA 相比,在到达第一个包含的输入元素时只会宣布一次)。

On the other hand, headings will not be announced at all by desktop screen readers when jumping between form elements using Tab / Shift-Tab .另一方面,当使用Tab / Shift-Tab在表单元素之间跳转时,桌面屏幕阅读器根本不会宣布标题。 So its information will easily be missed.所以它的信息很容易被遗漏。

What do you think about that, dear screen reader experts?亲爱的屏幕阅读器专家,您对此有何看法? Should we use aria-describedby to attach the headings to the subsequent <fieldset> ?我们应该使用aria-describedby将标题附加到随后的<fieldset>吗? What if there's more than one <fieldset> below that heading?如果该标题下方有多个<fieldset>怎么办?

I feel there's no perfect solution here.我觉得这里没有完美的解决方案。 Any suggestions, please?请问有什么建议吗?

It's perfectly valid to nest fieldset+legend, but as you ahve said, some screen readers will repeat it for each of the elements under that legend.嵌套字段集+图例是完全有效的,但正如您所说,一些屏幕阅读器会为该图例下的每个元素重复它。 It makes a lot of totally useless redundency.它产生了很多完全无用的冗余。

There is certainly no perfect solution, and no normative solution on this either, but I would go for an hybrid structure.当然没有完美的解决方案,也没有规范的解决方案,但我会 go 用于混合结构。 Use legend only for the deepest level, where elements are really close together such as male/female, yes/no, etc. (ie groups of radio buttons), and headings for all the rest and intermediate levels.仅将图例用于最深级别,其中元素非常接近,例如男性/女性、是/否等(即单选按钮组),以及所有 rest 和中间级别的标题。 For example:例如:

<h2>Personal information</h2>
<fieldset>
<legend>Gender</legend>
...
</fieldset>
<fieldset>
<legend>Marital status</legend>
...
</legend>
</fieldset>
<h2>Billing address</h2>
...
<h2>Delivery address</h2>
...

Headings have a great bonus advantage, there are shortcuts keys to jump directly to them.标题有很大的额外优势,有快捷键可以直接跳转到它们。 It's especially useful when you want to modify only one or two particular fields in the whole form.当您只想修改整个表单中的一两个特定字段时,它特别有用。 You can't do this with legend.你不能用传奇来做到这一点。

IF you are afraid that the screen reader user miss "Personal information", "Billing address" and "Delivery address", you may use aria-labelledby refering two IDs:

    <h2 id="billingAddressHeading">Billing address</h2>
        <p><label id="streetLabel" for=street">Street: </label>
        <input type="text" aria-labelledby="streetLabel billingAddressHeading"/>
        </p>
    
    If you use two IDs like that only on the first field of the group, you make sure that the screen reader won't uselessly read the group label for each of the fields.
    Be careful to not forget to reference the label itself in aria-labelledby, because it replaces completely the `<label>`.

Note that, technically, you are also allowed to use fieldsets without legen.

As a final note, if you find the form getting too complex, it's maybe the time to think about splitting it into several smaller pages.

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

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