如何使这个带标签的隐藏复选框可访问

huangapple go评论54阅读模式
英文:

How should I make this hidden checkbox with label accessible

问题

在一个由React生成的HTML表单中,我有一个带标签的复选框。复选框是隐藏的(不可见),检查/取消选中的方式是点击标签。目前,我已经设置了tabindex,以便在前一个表单控件之后将焦点放在标签上。然而,我找不到让用户只使用键盘来检查/取消选中复选框的方法,即使标签已经获得焦点。是否有人有任何方法来实现这一点?基本上,我希望有一种方式可以仅使用键盘来完成通常需要用鼠标点击标签才能完成的任务 - 检查/取消选中复选框。

以下是我使用的代码的基本想法:

注意:我添加了一个小的JavaScript片段,以在复选框更改时将其状态记录到控制台。

<div class="fields">
    <input type="text" id="other-field" tabindex="1">
    <input type="text" id="other-field-2" tabindex="2">
    <input type="checkbox" id="hidden-checkbox" tabindex="-1">
    <label id="label" for="hidden-checkbox" tabindex="3">点击这里切换!</label>
</div>
#hidden-checkbox {
    display: none;

    /* 也可以使用:
    position: absolute;
    clip: rect(1px, 1px, 1px, 1px);
    padding: 0;
    border: 0;
    height: 1px;
    width: 1px;
    overflow: hidden;
    */
}
document.getElementById("hidden-checkbox").onchange = function () {
    console.log(document.getElementById("hidden-checkbox").checked);
}
英文:

In an HTML form (being generated by React, but that's not relevant), I have a checkbox with a label. The checkbox is hidden (invisible), and the way to check/uncheck it is by clicking the label. At the moment, I have the tabindex set so that the label comes into focus after the previous form control. However, I can't find a way to let the user check/uncheck the checkbox using only the keyboard, even though the label is in focus. Does anyone have any idea how to do this? Basically I want there to be a way to achieve using only the keyboard what would usually be done by clicking the label with your mouse - checking/unchecking the checkbox.

Here is the basic idea of the code I'm using:

Note: I've added a small js snippet to log the state of the checkbox to the console whenever the checkbox is changed.

<!-- begin snippet: js -->

<!-- language: lang-html -->

&lt;div class=&quot;fields&quot;&gt;
    &lt;input type=&quot;text&quot; id=&quot;other-field&quot; tabindex=&quot;1&quot;&gt;
    &lt;input type=&quot;text&quot; id=&quot;other-field-2&quot; tabindex=&quot;2&quot;&gt;
    &lt;input type=&quot;checkbox&quot; id=&quot;hidden-checkbox&quot; tabindex=&quot;-1&quot;&gt;
    &lt;label id=&quot;label&quot; for=&quot;hidden-checkbox&quot; tabindex=&quot;3&quot;&gt;Click this to toggle!&lt;/label&gt;
&lt;/div&gt;

<!-- language: lang-css -->

#hidden-checkbox {
    display: none;

    /* Could also use:
    position: absolute;
    clip: rect(1px, 1px, 1px, 1px);
    padding: 0;
    border: 0;
    height: 1px;
    width: 1px;
    overflow: hidden;
    */
}

<!-- language: lang-js -->

document.getElementById(&quot;hidden-checkbox&quot;).onchange = function () {
    console.log(document.getElementById(&quot;hidden-checkbox&quot;).checked);
}

<!-- end snippet -->

答案1

得分: 3

你可以按照你的想法继续,将实际复选框从制表顺序中移除,并使标签在原位可聚焦,但请注意:

  • 你无法通过空格键或回车键轻松实现选中/取消选中的行为。你必须自己实现它。
  • 你必须在标签上使用ARIA,以指示给屏幕阅读器它是一个复选框及其状态,否则屏幕阅读器用户将不知道它的存在。

然而,你也可以更简单地让复选框保持正常的可聚焦状态。空格键和回车键将可以正常工作,而无需你做任何特殊处理。
屏幕阅读器用户将能够找到它,并能够像通常一样选中它。对于他们来说,它将“看起来”就像一个普通的复选框,不需要ARIA。

由于实际复选框在屏幕上是不可见的,你可以使用一些CSS或js来向用户指示选中状态和焦点状态。至少对于状态,你肯定已经在做了,所以这应该不是什么大问题。

英文:

You could keep going as you did with your concept of removing the actual checkbox from tab order and make the label focusable at its place, but be aware that:

  • You don't get the check/uncheck with spacebar or enter key behavior for free. You have to implement it yourself.
  • You must use ARIA on the label to indicate to screen readers that it's a checkbox and its state, otherwise screen reader users won't know about it

However, you you could do much simpler by letting the checkbox be normally focusable. Spacebar and enter will work without the need for you to do anything special.
Screen reader users will find it and will be able to check it as usual. To them, it will just "look" like a normal checkbox. No ARIA is needed.

Since the actual checkbox is invisible on the screen, you can use some CSS or js to indicate the checked state to the user and when it has the focus.
You are certainly already doing it at least for the state, so it shouldn't be a big deal.

答案2

得分: 2

不需要纠结于 tabIndex 或其他任何东西。当标签与复选框关联时,鼠标点击标签与点击复选框是相同的。即使在隐藏复选框后,你仍然希望它对仅使用键盘和屏幕阅读器的用户可用,该怎么办?方法是通过视觉隐藏它。

如果你将复选框的高度/宽度设置为0,它在视觉上将不可见,但在你使用Tab键或查找复选框时仍然可用,就像这样:

#hidden-checkbox{
 height: 0;
 width: 0;
 margin: 0;
 padding: 0;
}

如果你想看到它实际运行,这里有一个纯CSS的解决方案:
https://codepen.io/chart2music/pen/OJojagX

顺便说一句 -

在你的用例中,根本不需要 tabIndex。输入字段将自动接收Tab键焦点,因此不仅需要添加tabIndex属性,而且你可能会创建一个奇怪且不可预测的Tab键顺序。在何时使用它方面,这是一个经验法则:

  • 如果一个元素会接收焦点,但你不希望它接收焦点,使用 tabIndex=-1
  • 如果一个元素不会接收焦点,但你希望它接收焦点,使用 tabIndex=0
英文:

You don't need to fuss around with tabIndex or anything. When a label is associated with a checkbox, mouse clicking on the label is the same as clicking on the checkbox. How do you still make the checkbox available to keyboard-only and screen reader users, even after you hide it? By hiding it visually.

If you make the checkbox have a height/width of 0, it won't appear visually, but is still available when you tab around or look for checkboxes. Like this:

#hidden-checkbox{
 height: 0;
 width: 0;
 margin: 0;
 padding: 0;
}

If you want to see it in action, here's a pure CSS solution:
https://codepen.io/chart2music/pen/OJojagX

Side note -

Your use case doesn't need tabIndex at all, anywhere. Input fields will receive tab focus automatically, so not only do you not need to add the tabIndex attribute, you're likely to create a weird and unpredictable tab order. Here's your rule of thumb for when to use it:

  • If an element DOES receive focus, but you want it NOT to, use tabIndex=-1
  • If an element DOESN'T receive focus, but you want it TO, use tabIndex=0

huangapple
  • 本文由 发表于 2023年3月7日 10:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定