你能浏览所有的单选按钮吗?

我有一个单选按钮的列表。当我通过它们进行选项卡操作时,似乎只有第一个单选按钮或被选中的那个会得到关注,其余的单选按钮会被跳过。复选框没有这个问题。

Http://jsfiddle.net/2bd32/

我很难向我的质量保证人员解释这不是一个错误。有人能解释一下为什么会这样吗。

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"  />
101808 次浏览

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.

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.

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.

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).

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

I could not find answers anywhere, just parts of the solution or puzzle, so I decided to figure it out and prove it can be done. When you run the snippet below, do not select a radio button but rather click near to them, then hit the tab key, and you will see it selects the first one. Then it will select second and third etc.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="buttonArea" style="width:100%;height:100%;">
<label>Audi E-Tron</label><input type='radio' id='car1' name='car' value='Audi E-Tron'>
<label>Bolt EV</label><input type='radio' id='car2' name='car' value='Bolt EV'>
<label>Leaf EV</label><input type='radio' id='car3' name='car' value='Leaf EV'>
<label>Tesla Model 3</label><input type='radio' id='car4' name='car' value='Tesla Model 3'>
</div>
</body>
</html>


<script>
$(document).keydown(function(event) {


var car1 = document.getElementById("car1");
var car2 = document.getElementById("car2");
var car3 = document.getElementById("car3");
var car4 = document.getElementById("car4");


// 9 represents the tab keycode of the document keydown event.


event.preventDefault(); // this prevents the tab from doing its default action.


if(event.keyCode == 9 && car1.checked == false && car2.checked == false && car3.checked == false && car4.checked == false) {
car1.checked = true; // This checks the radio option.
}
else if (event.keyCode == 9 && car1.checked == true) {
car2.checked = true;
}
else if (event.keyCode == 9 && car2.checked == true) {
car3.checked = true;
}
else if (event.keyCode == 9 && car3.checked == true) {
car4.checked = true;
}
else if (event.keyCode == 9 && car4.checked == true) {
car1.checked = true;
}


car1.blur();    car2.blur();    car3.blur();    car4.blur();
})
</script>