简体   繁体   English

按键功能在javascript中不起作用

[英]Keypress function not working in javascript

I am trying to and have to keypress the letter 'A' with JavaScript. 我试图并且必须用JavaScript keypress字母“ A”。 In the code below, there is an alert('hello') box that indicates that the code is executing. 在下面的代码中,有一个alert('hello')框,指示代码正在执行。

But this code doesn't set any values to the "login-email" input (which is a textbox). 但是此代码没有为“ login-email”输入(这是一个文本框)设置任何值。

What can be wrong with the code? 代码有什么问题?

  function presskey() { var e = document.createEvent('KeyboardEvent'); if (e.initKeyboardEvent) { alert('hello'); e.initKeyboardEvent('keypress', true, true, document.defaultView, 'A', 0, '', false, ''); } document.getElementById('login-email').dispatchEvent(e); } 

First, I'd like to note that .initKeyboardEvent() is deprecated . 首先,我想指出.initKeyboardEvent()已弃用 What you likely want is the event constructor (Ex. for an 'input' event, new InputEvent() , as seen in the code below) 您可能想要的是事件构造函数(例如,对于“ input”事件,例如new InputEvent() ,如下面的代码所示)

That said, the rest of my answer assumes the question should actually be "How do I manually trigger input events on a textbox?". 就是说,我的其余答案都假定问题实际上应该是“如何手动触发文本框上的输入事件?”。 Please let me know if this isn't actually what you want, but the same concepts should apply to other types of events. 如果这不是您真正想要的,请告诉我,但是相同的概念也应适用于其他类型的事件。

...if this is your actual question, I can show you how I would start solving this problem: ...如果这是您的实际问题,我可以向您介绍如何开始解决此问题:

 const typeInto = (el, data) => { // Note the use of the InputEvent constructor const e = new InputEvent('input', { inputType: 'insertText', data, }) // Manually add the text to element.value el.value += data // Fire the event like you were doing el.dispatchEvent(e) } // Get element const el = document.querySelector('.js-login-email') // Add an event listener for the 'input' event, to prove it's working el.addEventListener('input', e => { console.log(`${e.inputType}: ${e.data}`) }) // Example "typeInto" usage: call for each letter in the string below 'example@example.com'.split('').forEach(letter => { typeInto(el, letter) }) 
 <input type="email" class="js-login-email"/> 

Mb you try addEventListener? 你尝试mbmb addEventListener?

elem.AddEventListener("keypress", (e) => {

    var e = document.createEvent('KeyboardEvent');

    if(e.keyCode === 65) {
        //...
    }

    document.getElementById('login-email').dispatchEvent(e);

});

you can make it by makeing keydown event then you can get your button keycode 您可以通过进行keydown事件来实现,然后可以获取按钮的键码

like this 像这样

window.addEventListener("keydown", function (event) {
  console.log(event.keyCode)
});

so in your code it will be like this 所以在你的代码中会像这样


window.addEventListener("keydown", function (event) {

  if(event.keyCode === 65){
     console.log('hello, world');
  }
}, true);

if you don't know which keycode you need to check this website will give you the code for each key 如果您不知道需要使用哪个键码,请访问网站,它将为您提供每个键的代码

or if you want do it from your key name you can make it like this 或者如果您想通过键名进行操作,可以像这样

window.addEventListener("keydown", function (event) {

  console.log(event.key)
});

NOTE : the key code for each button is come from assci code if you don't know what is it you can check this link 注意 :每个按钮的键代码来自于ASCII代码,如果您不知道它是什么,可以检查此链接

I have tried to combine the code now but are not sure if I do this right. 我现在尝试合并代码,但不确定是否正确。

Please notice that I want to programatically press the letter "A" into the 'login-email' box by running the presskey() function. 请注意,我要通过运行presskey()函数以编程方式将字母“ A”按入“登录电子邮件”框中。 I tried to run the below code but only the alert box is shown? 我试图运行以下代码,但仅显示警告框?

  function presskey() { var e = document.createEvent('KeyboardEvent'); if (e.initKeyboardEvent) { alert('hello'); e.initKeyboardEvent('keypress', true, true, document.defaultView, 'A', 0, '', false, ''); } document.getElementById('login-email').dispatchEvent(e); } var elem = document.getElementById('login-email'); elem.AddEventListener("keypress", (e) => { var e = document.createEvent('KeyboardEvent'); if(e.keyCode === 65) { //... } document.getElementById('login-email').dispatchEvent(e); }); 

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

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