[英]How to count the number of characters entered in a text box and display it automatically next to it?
我正在搜索以下事件,我需要在注册屏幕的文本框中输入的字符数自动出现在 JavaScript 或 jQuery 中的同一文本框旁边。
这是初学者的问题,提前致谢。
监听input
事件,然后检查文本框值的长度。
$("#input").on("input", function() { $("#count").text(this.value.length); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="input"> <span id="count">0</span>
恕我直言,按键应该是查找字符数的理想事件。
$('#input').on('keypress', function(e) {
var count = $(this).val().length;
$('#span').text(count);
})
Keypress 与 Keyup:
如果我按住任何字符键几秒钟,它会在输入框中输入几个字符,并且 keyup 事件会为此计数为 1,因为键仅被解除一次。 但是,按键会给出输入的确切字符数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.