简体   繁体   English

Chrome:使用 javascript 在输入文本字段上模拟按键事件

[英]Chrome: Simulate keypress events on input text field using javascript

There is a lot of contents on this in stack overflow but none seems to work for my case.堆栈溢出中有很多内容,但似乎没有一个适合我的情况。 I have an input text field and I want to simulate keypress event to fill the text field.我有一个输入文本字段,我想模拟按键事件来填充文本字段。

Reason: I am automating a lot of data entry task on a web interface which provides no API.原因:我在不提供 API 的 Web 界面上自动执行大量数据输入任务。 Changing the input field using .value does not trigger the JS side (angular) of the interface.使用.value更改输入字段不会触发界面的 JS 端(角度)。 That is why I want to simulate keypress event.这就是我想模拟按键事件的原因。

First I tried this:首先我尝试了这个:

var inp = document.getElementById('rule-type');
inp.dispatchEvent(new KeyboardEvent('keypress',{'key':'a'}));

Then I learned in Chrome the key and code stays as 0 and does not change in KeyBoardEvent .然后我在 Chrome 中了解到keycode保持为 0 并且在KeyBoardEvent中没有改变。

So I created seperate event ev = new KeyboardEvent('keypress',{'key':'a', 'code': 'KeyA'})所以我创建了单独的事件ev = new KeyboardEvent('keypress',{'key':'a', 'code': 'KeyA'})

And then I dispatched again, the return statement is true but it does not change the input field.然后我再次派发,return 语句为true ,但它不会更改输入字段。

The solution needs to be in pure javascript not jQuery.解决方案需要使用纯 javascript 而不是 jQuery。

You will not be able to fire an event that will cause text to populate an input.您将无法触发将导致文本填充输入的事件。 See this snippet from MDN :请参阅MDN的此片段:

Note: manually firing an event does not generate the default action associated with that event.注意:手动触发事件不会生成与该事件关联的默认操作。 For example, manually firing a key event does not cause that letter to appear in a focused text input.例如,手动触发键事件不会导致该字母出现在焦点文本输入中。 In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.对于 UI 事件,出于安全原因,这很重要,因为它可以防止脚本模拟与浏览器本身交互的用户操作。

Thanks @gforce301谢谢@gforce301

Your best bet may be to set the value and then dispatch an event.您最好的选择可能是设置该值,然后发送一个事件。

IMHO you're going about this all wrong.恕我直言,这一切都错了。 Angular is not "listening" to keypress or the like. Angular 不会“听” keypress等。 It is listening to change and input events.它正在侦听changeinput事件。 See the example below.请参见下面的示例。 It does (admittedly it is simple) what you need.它可以满足您的需求(诚然它很简单)。

 var iButton = document.getElementById('inputButton'); iButton.addEventListener('click', simulateInput); var cButton = document.getElementById('changeButton'); cButton.addEventListener('click', simulateChange); function simulateInput() { var inp = document.getElementById('name'); var ev = new Event('input'); inp.value =inp.value + 'a'; inp.dispatchEvent(ev); } function simulateChange() { var inp = document.getElementById('name'); var ev = new Event('change'); inp.value = 'changed'; inp.dispatchEvent(ev); }
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app=""> <p>Input something in the input box:</p> <p>Name : <input id="name" type="text" ng-model="name" placeholder="Enter name here"></p> <h1>Hello {{name}}</h1> </div> <button id="inputButton">I simulate the "input" event.</button> <button id="changeButton">I simulate the "change" event.</button>

To automate filling <input> and <textarea> values and automatically dispatch all needed events you need:要自动填充<input><textarea>值并自动调度您需要的所有需要​​的事件:

  1. focus the input\textarea聚焦输入\文本区域
  2. run document.execCommand with insertText command使用insertText命令运行document.execCommand
let inputElement = document.getElementsById('rule-type');
inputElement.focus();
document.execCommand('insertText', false, 'input value');

With this method input\textarea value modification should be captured by all major frameworks including Angular an Vuejs.使用这种方法 input\textarea 值修改应该被包括 Angular 和 Vuejs 在内的所有主要框架捕获。 This modification will be processed by frameworks the same way as if user pressed "Paste" option in browser main menu.此修改将由框架处理,就像用户在浏览器主菜单中按下“粘贴”选项一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM