gpt4 book ai didi

jquery - 如何在 html 文本输入框中输入文本时执行 jquery 函数?

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:45 25 4
gpt4 key购买 nike

我有这样的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com