简体   繁体   English

立即将输入值保存到变量

[英]Saving input value to a variable instantly

Hello,你好,

I need to instantly save the value of an input into a variable , adding keyup or keydown event listeners only updates the variable with the previous value of the input.我需要立即将输入的值保存到变量中,添加 keyup 或 keydown 事件侦听器仅使用输入的先前值更新变量。

Is there any better solution than using setTimeout() ?有没有比使用 setTimeout() 更好的解决方案?

document.getElementById('project').addEventListener('keydown', function projectNameVerifyInit() {
  let projectName = document.getElementById('project').value;
  console.log(projectName);
});

<input type="text" name="project" id="project" placeholder="Project Name" autofocus required>

Thanks谢谢

Use Input event.使用输入事件。

document.getElementById('project').addEventListener('input', function (evt) {
    let projectName = document.getElementById('project').value;
    console.log(projectName);
})

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

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