用jQuery自动按下键盘? [英] automatically press keyboard with jQuery?

查看:550
本文介绍了用jQuery自动按下键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery是否可以自动模拟键盘上的按键f.ex. html输入字段中?

Is it possible with jQuery to automatically simulate a press on a keyboard, f.ex. inside an html input field?

作为说明:如果我在输入字段中按 a ,则会在其中出现"a".我不想通过键盘来执行此操作,而是要通过按一个按钮来执行此操作.我知道我可以在输入上使用val("a"),但这不是我想要实现的.

As explanation: If I press the a inside an input field, an "a" will appear there. I would like to do this not from inside the keyboard, but by pressing a button. I know I could use val("a") on the input but this is not what I want to achieve.

我认为trigger()没有帮助,因为它仅调用keypress事件的事件处理程序.我不想打电话给处理程序,而是主动地按键盘而不敏锐地按下键盘!

I think trigger() doesn´t help because it only calls the event handler of a keypress event. I do not want to call the handler, but to actively press the keyboard without doing it acually!

推荐答案

我认为没有直接与键盘进行交互的javascript API.我建议您将字段值的更改与通常对应于键盘按下的事件处理程序触发结合起来,如您所提到的,以便按正常方式触发字段上的行为.

I don't think there is a javascript API for interacting directly with the keyboard. I would suggest combining a change in the field's value with an event handler firing that would normally correspond to the keyboard press, as you mentioned, so that behavior on the field is triggered as normal.

这篇关于用jQuery自动按下键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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