Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

基本表單應用

翻譯不完整。請協助 翻譯此英文文件

表單的 label

當使用傳統的 HTML 表單元素建立表單時,提供控制用的標籤(label)以及將標籤與對應表單元素建立關聯是非常重要的。當 screen reader (例如瀏覽器、電子郵件……等等)瀏覽一個頁面時,screen reader 會顯示 form controls ,但若沒有標示 control 和 label 之間的關聯,  screen reader 沒法知道哪個 label 是對應哪個 control。

下面的範例顯示一個使用標籤的表單。注意每一個 <input> 元件都有 id,每一個 <label> 元件有 for 屬性,用來對應 <input> 元素的 id 。

範例 1. 使用 label 的簡易表單

<form>
  <ul>
    <li>
      <input id="wine-1" type="checkbox" value="riesling"/>
      <label for="wine-1">Berg Rottland Riesling</label>
    </li>
    <li>
      <input id="wine-2" type="checkbox" value="weissbergunder"/>
      <label for="wine-2">Weissbergunder</label>
    </li>
    <li>
      <input id="wine-3" type="checkbox" value="pinot-grigio"/>
      <label for="wine-3">Pinot Grigio</label>
    </li>
    <li>
      <input id="wine-4" type="checkbox" value="gewurztraminer"/>
      <label for="wine-4">Berg Rottland Riesling</label>
    </li>
  </ul>
</form>

Labeling with ARIA

The HTML <label> element is appropriate for form-related elements, but many form controls are implemented as a dynamic JavaScript widget, using <div>s or <span>s. WAI-ARIA, the Accessible Rich Internet Applications specification from the W3C's Web Accessibility Initiative, provides the aria-labelledby attribute for these cases.

The example below shows a radio button group implemented using an unordered list. Note that on line 3, the <li> element sets the aria-labelledby attribute to "rg1_label," the id of the <h3> element on line 1, which is the label for the radio group.

Example 2. A radio group implemented using an unordered list (adapted from https://www.oaa-accessibility.org/examplep/radio1/)

<h3 id="rg1_label">Lunch Options</h3>

<ul class="radiogroup" id="rg1"  role="radiogroup" aria-labelledby="rg1_label">
  <li id="r1"  tabindex="-1" role="radio" aria-checked="false">
    <img role="presentation" src="radio-unchecked.gif" /> Thai
  </li>
  <li id="r2"  tabindex="-1" role="radio"  aria-checked="false">
    <img role="presentation" src="radio-unchecked.gif" /> Subway
  </li>
  <li id="r3"   tabindex="0" role="radio" aria-checked="true">
    <img role="presentation" src="radio-checked.gif" /> Radio Maria
  </li>
</ul>  

Describing with ARIA

Form controls sometimes have a description associated with them, in addition to the label. ARIA provides the aria-describedby attribute to directly associate the description with the control.

The example below shows a <button> element that is described by a sentence in a separate <div> element. The aria-describedby attribute on the <button> references the id of the <div>.

Example 3. A button described by a separate element.

<button aria-describedby="descriptionRevert">Revert</button>
<div id="descriptionRevert">Reverting will undo any changes that have been made
                            since the last save.</div>

(Note that the aria-describedby attribute is used for other purposes, in addition to form controls.)

Required and invalid fields

Web developers typically use presentational strategies to indicated required or invalid fields, but assistive technologies (ATs) cannot necessarily infer this information from the presentation. ARIA provides attributes for indicating that form controls are required or invalid:

  • The aria-required property can be applied to a form element to indicate to an AT that it is required to complete the form.
  • The aria-invalid state can be programmatically applied to indicate to an AT which data fields have incorrect data, so that the user knows they have entered invalid data.

The example below shows a simple form with three fields. On lines 4 and 12, the aria-required attributes are set to true (in addition to the asterisks next to the labels) indicating that the name and email fields are required. The second part of the example is a snippet of JavaScript that validates the email format and sets the aria-invalid attribute of the email field (line 12 of the HTML) according to the result (in addition to changing the presentation of the element).

Example 4a. A form with required fields.

<form>
  <div>
    <label for="name">* Name:</label>
    <input type="text" value="name" id="name" aria-required="true"/>
  </div>
  <div>
    <label for="phone">Phone:</label>
    <input type="text" value="phone" id="phone" aria-required="false"/>
  </div>
  <div>
    <label for="email">* E-mail:</label>
    <input type="text" value="email" id="email" aria-required="true"/>
  </div>
</form>

Example 4b. Part of a script that validates the form entry.

var validate = function () {
  var emailElement = document.getElementById(emailFieldId);
  var valid = emailValid(formData.email); // returns true if valid, false otherwise

  emailElement.setAttribute("aria-invalid", !valid);
  setElementBorderColour(emailElement, valid); // sets the border to red if second arg is false
};

提供有幫助的錯誤訊息

繼續閱讀了解如何使用 ARIA alerts to enhance forms.

TBD: we should either combine into one article or separate into techniques, or both. Also, is ARIA markup appropriate for error messages in a page loaded after server side validation?

參閱 WAI-ARIA Authoring Practices .

文件標籤與貢獻者

標籤: 
 此頁面的貢獻者: Shiyou, alk03073135
 最近更新: Shiyou,