通过按Enter键确定是否从HTML 5 datalist中选择了一个元素 [英] Determine if an element was selected from HTML 5 datalist by pressing enter key

查看:102
本文介绍了通过按Enter键确定是否从HTML 5 datalist中选择了一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery(document.body).on('input', '.icdCodeInput', function (event) {

});

我有一个带有类icdCodeInput的HTML 5数据表。通过使用鼠标或按Enter键从列表中选择项目时,上述事件将被触发。如果通过按Enter键选择项目,我如何区分事件?

I have a HTML 5 datalist with class icdCodeInput. When I select an item from the list by either using mouse or by pressing enter, the above event gets fired. How can I differentiate inside the event if the item was selected by hitting enter key ?

推荐答案

不幸的是,目前这不可能在所有。 输入事件仅在输入中的值实际更改后才会触发,这在进行选择后总会出现。该事件不包含有关如何添加此信息的信息。

Unfortunately, this is currently not possible at all. The input event is only fired once the value inside the input actually changes, which always comes after making your selection. The event does not hold information on how this was added.

另一种方法是跟踪实际的点击或按键事件。但是,在使用datalist时(至少在Chrome中),似乎没有触发click事件或任何按键事件。即使您将它们绑定在文档或正文上,点击或按键也只是在实际数据列表上执行时不会注册。

An alternative would be to track the actual click or keypress events. However, neither the click event nor any of the keypress events seem to be fired when using the datalist (at least not in Chrome). Even if you bind them on the document or body, clicks or keypresses simply do not register when performed on the actual datalist.

因此,无法区分它们在这些选择datalist选项的方法之间。

As such, it is impossible to tell the difference between these methods of selecting a datalist option.

这篇关于通过按Enter键确定是否从HTML 5 datalist中选择了一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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