为什么 keyup/keydown 不会在“选择”时触发?

the*_*hai 6 javascript keyboard-events

当客户端在选项之间移动时,为什么不会触发键盘事件select?是否还发送了其他事件?

如何重现:

  • 聚焦于选择的元素。
  • 打开选择的选项。
  • 使用键盘(向上/向下)箭头在选择选项中移动。

结果:当您使用向上/向下键盘键浏览选项时,不会发送任何事件。

超文本标记语言

<!-- The second value will be selected initially -->
<select name="text"> <!--Supplement an id here instead of using 'text'-->
  <option value="value1">Value 1</option> 
  <option value="value2" selected>Value 2</option>
  <option value="value3">Value 3</option>
</select>
Run Code Online (Sandbox Code Playgroud)

JS

function onkeyEvent(evt ){
    console.log(evt);
}
            window.addEventListener('keyup', onkeyEvent, true);
            window.addEventListener('keydown', onkeyEvent, true);
            window.addEventListener('keypress', onkeyEvent, true);
Run Code Online (Sandbox Code Playgroud)

仅在选择选项后才会触发更改事件,而不是在客户端选择(在选项中移动)时触发,因此我不接受它作为答案。

Meg*_*han -2

正如其他人提到的,要从<select>元素获取关键事件,您可以向元素的change事件添加事件侦听器。