本文介绍了JavaScript-在< input>上运行函数变化-没有JQUERY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML <input/>标记更改时运行一个函数.我该怎么做呢?对此有一些答案,但是它们都在jQuery中,我想要纯JavaScript.有人可以给我一个简单的例子吗?谢谢!

I want to run a function when a HTML <input/> tag changes. How do I do this? There are some answers for this but they are all in jQuery and I want plain javascript. Can someone give me a simple example? Thanks!

我希望输入为文本类型:

I want the input to be a type as text:

<input type="text" id="change">

推荐答案

您可以使用输入事件.这不仅会触发按键和粘贴操作,还会触发其他文本修改事件,例如拖动和粘贴;放下.

You can use the input event. This will trigger not only on key up and paste, but also other text modification events such as drag & drop.

change.addEventListener("input", function (e) {
    alert(this.value);
});
<input type="text" id="change">

这篇关于JavaScript-在&lt; input&gt;上运行函数变化-没有JQUERY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:27