我有这样如何在html文本输入框中输入文本时执行jquery函数?
<input type="text" class="input-box">
我希望在输入框装满4个字符,执行一个jQuery功能的HTML代码。
例如,我键入“ABCD”,那么该函数将被执行,而不是“AB”或“ABC”类似的东西。我怎样才能做到这一点?
我有这样如何在html文本输入框中输入文本时执行jquery函数?
<input type="text" class="input-box">
我希望在输入框装满4个字符,执行一个jQuery功能的HTML代码。
例如,我键入“ABCD”,那么该函数将被执行,而不是“AB”或“ABC”类似的东西。我怎样才能做到这一点?
绑定的事件处理程序,并检查文本长度的基础上,长度调用函数
$('.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">
谢谢,@pranav。得到我想要的。 –
@ShafeequeS:很高兴帮助你 –
@ShafeequeS如果这回答你的问题,请[接受答案](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) – mplungjan
http://stackoverflow.com/questions/17384218/jquery-input-event – mplungjan