通过js实时检测文本框内容

思路

1,在获取文本框焦点后,启动定时器,每隔100毫秒来查看文本内容的改变
2,在文本框失去焦点后,清除定时器

下面是一个简单的例子

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>input 监听</title>
</head>
<body>
	<label>
		<span>姓名</span>
		<input id="input" type="text">
	</label>
	<p id="input-tip"></p>

	<script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script>
	<script type="text/javascript">
		$("#input").focus(function(){
			time = setInterval(function(){
				var value = $("#input").val();
				$("#input-tip").text(value);
				console.log("setInterval");
			},100);
		}).blur(function(){
			console.log("clearInterval");
			clearInterval(time);
		})
	</script>
</body>
</html>

原文地址:https://www.cnblogs.com/geek12/p/5260362.html