onkeyup或onkeypress仅在X个字符长度之后

onkeyup or onkeypress only after X characters length

本文关键字:字符 之后 onkeypress 仅在 onkeyup      更新时间:2023-09-26

只有当我们在文本字段上填充多个字符时,才可以调用js函数吗?

从现在起,我使用onblur="myFunction();",但我想在填充一些字符后调用该函数。

谢谢!

使用onkeyup事件:

let element = document.querySelector('your element');
element.onkeyup = function () {
    if (this.value.length > 10) {
        myFunction();
    }
}

你也可以在属性中使用这个:

<textarea onkeyup="if (this.value.length > 10) myFunction();"></textarea>

示例(对于两者)

/* go automatic do the next tabindex */ 
$(":input:not(:disabled)").not($(":button")).keyup(function(evt){
if (this.value.length == 1) {
    currentTabindex = $(this).attr('tabindex');
            if (currentTabindex) {
                nextInput = $('input[tabindex^="'+ (parseInt(currentTabindex)+1) +'"]');
                if (nextInput.length) {
                    nextInput.focus();
                    return false;
                }
            }
}
});