问题 The first time a keypress event fires, it logs an empty input value even though the input has a value. The second time it logs the value but is one keystroke behind in comparison with the input's value. You can check this behavior on the next example: document.addEventListener('DOMContentLoaded', () => { const input = document.querySelector('input'); input.addEventListener('keypress', e => { console.log(e.target.value); }); }); <input type="text"/> However, the next workaround makes it work,