Pitfalls of Simulating Keyboard Input with JS
Pitfalls At first I searched around on Google and found a jQuery solution. The amount of code was small, it looked great and promising, but it didn't work at all…… I tried these versions over and over: var e = $.Event('keyup') e.keyCode = 49 $('#btn').trigger(e) // just replace keycode with which $('#btn').trigger($.Event('keyup', { which: 4
目录4 个章节
Pitfalls
At first I searched around on Google and found a jQuery solution. The amount of code was small, it looked great and promising, but it didn't work at all……
I tried these versions over and over:
var e = $.Event('keyup')
e.keyCode = 49
$('#btn').trigger(e)
// 就是把 keycode 换成 which
$('#btn').trigger($.Event('keyup', { which: 49 }))
Didn't work at all……
Then I looked at the event listener in the developer debugging tools and found that the input box was listening for the input event. So I tried setting the input's value first, then using jQuery to simulate triggering the event to see if it would work~
$('#btn').trigger('input')
That didn't work either
Solution
Then I found a pretty good method: using native JS InputEvent. For older Chrome, use UIEvent instead; the effect is basically the same.
The code is as follows
Using InputEvent:
var dom = document.querySelector('#selector')
var evt = new InputEvent('input', {
inputType: 'insertText',
data: st,
dataTransfer: null,
isComposing: false
});
dom.value = '输入的内容';
dom.dispatchEvent(evt);
Using UIEvent:
var dom = document.querySelector('#selector')
var evt = new UIEvent('input', {
bubbles: false,
cancelable: false
});
dom.value = st;
dom.dispatchEvent(evt);
Personally tested, it works perfectly
You can wrap it into a function yourself for more convenient calling; I won't paste that part of the code~
References
- Useful reference: http://fengyitong.name/index.php/archives/38/
- Simulating keyboard input with js+JQuery: https://blog.csdn.net/qq_45741976/article/details/102645358
- A simple website for getting keycodes: https://keycode.info/
Welcome to Discuss
Programming Lab focuses on exploring popular new internet technologies and team agile development practices. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

- Cnblogs: https://www.cnblogs.com/deali/
- Coding livestream: https://live.bilibili.com/11883038
- Zhihu: https://www.zhihu.com/people/dealiaxy
_EOF_
Pitfalls of Simulating Keyboard Input with JS
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Vue2.x项目整合ExceptionLess监控
前言 一直以来我们都是用Sentry做项目监控,不过前段时间我们的Sentry坏掉了~~(我搞坏的)~~ 但监控又是很有必要的,在sentry修好之前,我想先寻找一个临时的替代方案,同时发现网上关于ExceptionLess的资料少得可怜,ExceptionLess官方的文档也不是很完善,翻了好久文档和源码,于是有了本文…… 关于ExceptionLess 写.NetCore的同学应该不会陌生,这
2022-03-23 · Web前端 · 约 1 分钟
在网页中使用Material-Design图标
什么是Material Design图标 MD大家都听过的吧,就是谷歌的Material Design设计,Material Design图标就是符合 MD设计语言的图标。 本文通过字体的方法在网页中显示Material Design图标。 What are material icons Material design system icons are simple, modern, friend
2021-12-18 · Web前端 · 约 1 分钟
在Next.js中集成swagger文档
Next.js 本来就不能直接用 Swagger,非要用还得自己折腾 API Routes?自动生成工具?手动写 Spec?这简直是在侮辱开发者。手动注释?这操作简直是在挑战耐心极限。
2025-09-01 · Web前端 · 约 1 分钟
使用mock.js开发假数据接口
参考资料 Mockjs使用 - https://juejin.cn/post/6953199521031520292 http://wll8.gitee.io/mockjs-examples Mokcjs 文档 Vitepress版 - http://mock.pe666.cn/
2022-12-06 · Web前端 · 约 1 分钟
讨论
共 0 条加载评论中…