keydown事件在下拉列表中 [英] keydown event in drop down list

查看:132
本文介绍了keydown事件在下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个下拉列表,当用户持有SHIFT键时,它将在所有其他下拉列表中选择相同的索引。



目前,我正在做以下工作:

  $(document).on('keyup keydown',function(e){shifted = e.shiftKey}); 
$(document).on('change','.report_info select',function(e){
if(shifted){
//用于更改其他下拉列表的代码
}
});

只有在您输入下拉列表之前按住Shift键,才能使用此功能。如果你在DDL中并且按shift键,那么keyup / keydown事件将不会触发,并且 shifted 将保持为 false



是否有任何方法可以在关注下拉列表时捕捉keyup / keydown事件?

编辑:



看起来像只适用于 Chrome 以下是它在Firefox和IE中的作用,但不适用于Chrome:

  $(document).on('keyup keydown' ,'select',函数(e){
shifted = e.shiftKey;
});

下面是它在chrome中不工作的小提琴: http://jsfiddle.net/ue6xqm1q/4

解决方案

我认为@法官的回应可能是你最好的选择。我发布这个答案而不是评论,因为我已经做了自己的研究,以确定当 select 元素打开时绝对不会触发事件在Chrome中。



参见小提琴: http:// jsfiddle .net / m4tndtu4 / 6 /

我将所有可能的事件处理程序(我认为)附加到 input 元素和选择元素。



在Chrome中,工作时会看到许多事件触发使用 input 元素,但在第一个元素打开时选择元素时,您将看不到任何事件触发。



有趣的是,如果 select 元素的多个属性或大小> 1。



你会看到事件触发了所有三个元素。



Outside @ judgeja的建议,你最好的选择可能是模仿选择元素。


I am trying to create a Drop down list, that when a user holds the SHIFT key, it will select the same index on all other drop down lists.

Currently, I am doing the following:

$(document).on('keyup keydown', function (e) { shifted = e.shiftKey });
$(document).on('change', '.report_info select', function (e) {
    if (shifted) {
        //Code to change other drop down lists.
    }
});

This only works if you press and hold the shift key before you enter the drop down list. If you are inside the DDL and press the shift key, the keyup/keydown event will not fire and shifted will remain false

Is there any way to catch the keyup/keydown event while a dropdownlist is focused?

Edit:

Looks like it might be an issue with Chrome only, Just tried adding the following, and it works in Firefox and IE, but not Chrome:

$(document).on('keyup keydown', 'select', function (e) {
    shifted = e.shiftKey;
});

Here is a fiddle of it not working in chrome: http://jsfiddle.net/ue6xqm1q/4

解决方案

I think @judgeja's response may be your best bet. I'm posting this as an "answer" instead of a comment, because I've done my own research to determine that absolutely no event gets fired when a select element is open in Chrome.

See Fiddle: http://jsfiddle.net/m4tndtu4/6/

I've attached all possible event handlers (I think) to the input element and both select elements.

In Chrome, you'll see many events fire when working with the input element, but you'll see no events fire when working in the first select element when it is open.

Interestingly, events do fire in Chrome if the select element has the multiple attribute or size>1.

In Firefox, you'll see events firing on all three elements.

Outside @judgeja's suggestion, your best bet may be to simulate the select element.

这篇关于keydown事件在下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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