简体   繁体   中英

web accessibility : html native dropdown - open menu items on enter click

on select enter click , by default form gets submitted on.As per accessibility criteria, if focus is on select element on enter click it should open list items.I want to know is it possible to make accessable dropdown using native select element which will meet accessibility criteria

<div class="custom-dropdown">
    <select id="cities" name="select">
       <option value="1">Delhi</option>
       <option value="2">Mumbai</option>
</select>``
</div>

I have prevented default submission behaviour.

$('.custom-dropdown').keydown(function (event) {
      if (event.keyCode == 13) {
        event.preventDefault();
        return false;
      }
});

I have tried keyup event to trigger click event but its not working

$('#cities').keyup(function (e) {
      if (e.keyCode == 13) {
        $("#cities").trigger("click");
      }
    });

I'm pretty sure the only element that will submit a form automatically is a submit button.

<input type="submit" value="foo">

or

<button>foo</button>

(the default type for a <button> element is "submit")

The <select> is not defined as submitting a form. Are you sure your focus is on the <select> when you press enter ?

You should also look at the specs for " 4.10.18.6. Form submission "

You're basing your concern on an example , not the specification:

The example listbox on this page implements the following keyboard interface

If you refer to Keyboard Interaction for the Listbox Design Pattern , the Enter key is not mentioned.

The example uses Enter , because the trigger for the listbox is a Button.

Also, in general, ARIA rules are meant for cases where you cannot use standard elements, according to the First rule of ARIA use

If you can use a native HTML element [HTML51] or attribute with the semantics and behavior you require already built in, instead of re-purposing an element and adding an ARIA role, state or property to make it accessible, then do so.

In other words, you shouldn't care about those rules, when you don't implement your own controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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