在 Javascript 中更改的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3915164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:41:05  来源:igfitidea点击:

On text changed in Javascript

javascriptformstextbox

提问by Chris

i need to be able to able to detect when any character is entered into a textbox in Javascript, and clear another corresponding textbox. I have used the onfocus method for this but when tabbing between textboxes to get to submit, it removes the data and I don't want it to.

我需要能够检测到任何字符何时输入到 Javascript 中的文本框,并清除另一个相应的文本框。我为此使用了 onfocus 方法,但是当在文本框之间切换以提交时,它删除了数据,我不希望它这样做。

What methods are there I can use to trigger a JS event when a textbox is entered into?

当输入文本框时,我可以使用哪些方法来触发 JS 事件?

回答by John Hartsock

There are the following events

有以下事件

onkeydown

按键按下

onkeyup

上键

onkeypress

按键

回答by Bonshington

If you use jQuery:

如果您使用 jQuery:

$("#myTextbox").keypress()
$("#myTextbox").keyup()
$("#myTextbox").keydown()

and if you want to find out which key was pressed, try:

如果您想找出按下了哪个键,请尝试:

$("#myTextbox").keydown(function(e){ alert(e.which); });

if you're not using e.which, you're gonna have cross-browser problems.

如果您不使用 e.which,则会遇到跨浏览器问题。