我有这样的 HTML 代码
<input type="text" class="input-box">
我想在输入框填满 4 个字符时执行一个 jQuery 函数。
例如,我输入“ABCD”然后函数将被执行,而不是“AB”或“ABC”之类的东西。我该怎么做?
最佳答案
绑定(bind)事件处理程序并检查文本长度,根据长度调用函数
$('.input-box').on('input', function() {
if (this.value.trim().length >= 4) {
// call your function here
doIt();
}
});
function doIt() {
alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="input-box">
关于jquery - 如何在 html 文本输入框中输入文本时执行 jquery 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37189562/