与
<input type="text" onKeyPress="alert(this.value)">
警告框显示框中的值,不包括当前按下的键。
但是,这并没有表现出相同的行为,我不知道为什么。
<input type="text" onKeyPress="handleInput(this.value)">
function handleInput(value){
alert(value);
}
最佳答案
首先要避免在 html 标签中使用 js 代码,这是一种不好的做法 , 你可以使用 keyup 事件在用户释放任何按下的键后获取输入的内容
<input id="input">
<script>
var input = document.getElementById('input');
input.addEventListener('keyup',function(){alert(input.value);});
</script>
关于javascript - 如何从javascript中的文本输入按键调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788896/