JavaScript 使用HTML DOM的oninput事件,实时监听value值变化

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<p>该实例演示了如何使用 addEventListener() 方法向 input 元素中添加 "oninput" 事件。</p>
<p>向文本框中尝试输入即可触发事件。</p>
输入您的名字: <input type="text" id="myInput" value="Mickey">
<script>
document.getElementById("myInput").addEventListener("input", myFunction);
function myFunction() {
    alert("input 输入框值已发生变化。");
}
</script>

</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<p>该实例演示了如何使用 HTML DOM 向 input 元素中添加 "oninput" 事件。</p>
<p>向文本框中尝试输入即可触发事件。</p>
输入您的名字:  <input type="text" id="myInput" value="Mickey">
<script>
document.getElementById("myInput").oninput = function() {myFunction()};
function myFunction() {
    alert("input 输入框值已发生变化。");
}
</script>

</body>
</html>

感谢菜鸟教程runoob.com

原文地址:https://www.cnblogs.com/pangpanghuan/p/6525053.html