问题
I have a list of radio buttons. when I tab through them, seems like only the first radio button or the one that is selected will get focused, the rest of the radio buttons will get skipped. checkbox didnt have this problem.
http://jsfiddle.net/2Bd32/
I have a hard time to explain to my QA this is not a bug. can someone please explain to me why this happens.
Soccer: <input type="checkbox" name="sports" value="soccer" tabindex="1" /><br />
Football: <input type="checkbox" name="sports" value="football" tabindex="2" /><br />
<input type="radio" name="num" value="3" tabindex="3">3<br>
<input type="radio" name="num" value="4" tabindex="4">4<br>
<input type="radio" name="num" value="5" tabindex="5">5<br>
<input type="radio" name="num" value="6" tabindex="6">6<br>
<input type="radio" name="num" value="7" tabindex="7">7<br>
Baseball: <input type="checkbox" name="sports" value="baseball" tabindex="8" /><br />
Basketball: <input type="checkbox" name="sports" value="basketball" tabindex="9" />
回答1:
You can cite the W3C as your source, here and here.
Essentially a radio button is a group that functions as a single element since it retains only a single value. Tabbing to a radio group will bring you to the first item and then using the arrow keys you navigate within the group.
- Focus can move to a radio group via:
- The Tab key
- An Access Key or mnemonic targeting the label
- Activation of the label (through Assistive Technology mechanism)
- The Tab key moves focus between radio button groups and other widgets.
- When focus is on the group and when no radio button is selected:
- Tab key press moves focus to the first radio button in the group, but does not select the radio button.
- Shift+Tab key press moves focus to the last radio button in the group, but does not select the radio button.
- When focus moves to the group in which a radio button is selected, pressing Tab and Shift+Tab keys move focus to the radio button that is checked.
- Up Arrow and Down Arrow keys move focus and selection.
- Up Arrow key press moves focus and selection forward through the radio buttons in the group. If the first radio button has focus, then focus and selection move to the last radio button in the group.
- Down Arrow key press moves focus and selection backward through the radio buttons in the group. If the last radio button has focus, then focus and selection move to the first radio button in the group. * Space Bar key press checks the radio button that currently has focus.
- When re-entering the group, focus returns to the point of previous focus (the item with selection set).
- Pressing the Tab key exits the group and moves focus to the next form control.
- Pressing the Shift+Tab keys exits the group and moves focus to the previous form control.
回答2:
Radio buttons with one name is like single control (like input or select), you can change it value with arrow keys, tab key moves focus to another control.
回答3:
If you are using angularjs, you can control radio button directly with ng-model
by using same ng-model
and removing name
.
<input type="radio" value="0" ng-model="status">
<input type="radio" value="1" ng-model="status">
Now one more advantage beside tab press is that you can first focus through the radio button without actually selecting it. You can then select by pressing space.
回答4:
Not sure if this is still an issue for anyone but I found a solution. As stated above, this is not a bug, this is default behavior. We just need to think of how we interact with a website differently. When you tab into a radio group of objects, you need to then use the arrow keys to navigate within that set of inputs and then tab or shift+tab to get out of that set of inputs.
https://www.w3.org/TR/wai-aria-practices/examples/radio/radio-1/radio-1.html
回答5:
If you are using Angular Material for your application, you can enable radio button tabbing in a cross-browser compatible way by adding a tabindex
to each <mat-radio-button>
:
<mat-radio-button [tabindex]="0">
Otherwise you will be unable to tab to radio buttons in Safari (because this is the default browser behavior - click here for one discussion).
来源:https://stackoverflow.com/questions/14322564/can-you-tab-through-all-radio-buttons