我有這樣如何在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