防止在选择中向右/向左箭头导航 [英] Prevent right/left arrow navigation in select

查看:80
本文介绍了防止在选择中向右/向左箭头导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个选择可以启用键盘导航(左右箭头键)在它们之间移动.按向右或向左箭头键时的默认行为是向上/向下移动列表.

I have 3 selects that I am enabling keyboard navigation(right and left arrow keys) to move between them. The default behavior when pressing the right or left arrow key is to move up/down the list.

我的解决方案可以在选择之间移动,但是每次您移动到新选择时,刚刚移动的选择中的所选值都会根据您按下的箭头向上或向下移动.

My solution works moving between the selects, but every time you move to a new select, the selected value in the select that you just moved from gets moved up or down depending on which arrow you pressed.

有没有一种方法可以防止选择的默认左右箭头键?我已经尝试过e.preventPropagation和e.preventDefault,但这似乎不起作用.

Is there a way to prevent the default right and left arrow keys for the selects? I have tried e.preventPropagation and e.preventDefault but that doesn't seem to work.

JSFIDDLE

推荐答案

我刚刚弄清楚了.当我需要绑定到keydown事件时,我正在绑定keypress事件.绑定到keydown时,可以使用e.preventDefault().

I just figured it out. I was binding to keypress event, when I needed to bind to the keydown event. When binding to keydown then you can use e.preventDefault().

JSFIDDLE解决方案

这篇关于防止在选择中向右/向左箭头导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆