gpt4 book ai didi

javascript - 如何从javascript中的文本输入按键调用函数?

转载 作者:行者123 更新时间:2023-11-30 07:23:24 24 4
gpt4 key购买 nike

<input type="text" onKeyPress="alert(this.value)">

警告框显示框中的值,不包括当前按下的键。

但是,这并没有表现出相同的行为,我不知道为什么。

<input type="text" onKeyPress="handleInput(this.value)">

function handleInput(value){
alert(value);
}

添加了这个 http://jsfiddle.net/abalter/adbbb599/6/

最佳答案

首先要避免在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com