简体   繁体   English

HTML表单可访问性:Tab键焦点将跳过新显示的字段

[英]HTML form accessibility: tab key focus skips newly shown field

I have a form that contains a select field that is only shown when the value of the previous select field is set to a specific value. 我有一个包含选择字段的表单,仅当前一个选择字段的值设置为特定值时才会显示。 Here is a simplified version where a hidden "states" select field appears if "USA" is selected as the country: 这是简化版本,如果选择“美国”作为国家,则会显示一个隐藏的“州”选择字段:

HTML: HTML:

<style>
  label {
    display:inline-block;    
    min-width:100px;        
    padding:3px;
  }
  .hidden{
    display:none;
  }
</style>

<div>
  <label for="country">Country</label>
  <select id="country">
    <option>Czech Republic</option>
    <option>USA</option>
  </select>
</div>
<div id="stateContainer"  class="hidden">
  <label for="state">State</label>
  <select id="state">
    <option>Alabama</option>
    <option>Arizona</option>
    <option>Arkansas</option>
  </select>
</div>
<div>
  <label for="name">Name</label>
  <input type="text" id="name" />
</div>

JS: JS:

$(function(){
  $("#country").on("change",function(){
    if ( $(this).val() === "USA") {
      $("#stateContainer").removeClass("hidden");
    } else {
      $("#stateContainer").addClass("hidden");
    }
  })
})

...and here it is in JSFiddle: ...在JSFiddle中:

http://jsfiddle.net/xoundboy/a2HdZ/5/ http://jsfiddle.net/xoundboy/a2HdZ/5/

it works fine when using a mouse but if the user is restricted to keyboard input only then UX is broken. 使用鼠标时,它工作正常,但如果仅将用户限制为键盘输入,则UX损坏。 You can see this if focus is in the country field and you use the up/down arrows to select "USA" as the country and then the tab key to move to the next field. 如果焦点在“国家/地区”字段中,并且使用向上/向下箭头选择“ USA”作为国家/地区,然后使用Tab键移动到下一个字段,则可以看到此信息。 Instead of moving to the newly shown "state" field, it skips over to the "name" field. 它没有跳到新显示的“状态”字段,而是跳到“名称”字段。

I could probably fix it by capturing the tab key press in JS, forcing the focus onto the newly shown field and preventing default behaviour but that seems like an ugly hack. 我可能可以通过捕获JS中的Tab键按下,将焦点集中到新显示的字段上并防止出现默认行为来解决此问题,但这似乎是一个丑陋的hack。 What is the best practice for handling what must be quite a common scenario? 处理最常见的情况的最佳实践是什么?

The change event is not triggered when up/down key is pressed. 按下向上/向下键不会触发更改事件。 So the change event can be triggered on a key press. 因此,可以通过按键触发change事件。

js fiddle example for the code changes. js小提琴示例的代码更改。 Hope it helps. 希望能帮助到你。

EDIT 1 I checked with different browsers and found out that this problem is in firefox and hence the solution is for firefox. 编辑1我检查了不同的浏览器,发现此问题在Firefox中,因此解决方案是在Firefox中。 For chrome the fiddle in question works perfectly fine for me. 对于chrome,该小提琴对我来说效果很好。

EDIT 2 My previous fiddle had change triggered on keypress . 编辑2我以前的小提琴在keypress触发了change It seems firefox changed somethings and it no more works. 似乎Firefox改变了一些东西,它不再起作用。 I have updated the event to keyup . 我已将事件更新为keyup

just use the tabindex element property, tab order goes from lowest to highest value 只需使用tabindex元素属性,制表符的顺序就从最低到最高

Fiddle 小提琴

<div>
  <label for="country">Country</label>
  <select id="country" tabindex=1>
    <option>Czech Republic</option>
    <option>USA</option>
  </select>
</div>
<div id="stateContainer"  class="hidden">
  <label for="state">State</label>
  <select id="state" tabindex=2>
    <option>Alabama</option>
    <option>Arizona</option>
    <option>Arkansas</option>
  </select>
</div>
<div>
  <label for="name">Name</label>
  <input type="text" id="name" tabindex=3 />
</div>

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

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