문제

Well , this my code , i'm trying to add to the enter button a tab behavior( when i click on enter , the next input will focus).

When i press the enter key , an Uncaught RangeError appears. I tried everything , but nothing changed.

Environment : JavaEE( primefaces ). jquery.

function ifNotTextPass(selector) {
    if (selector.next().is('input[type="text"]')) {
        alert('yes');
        selector.next().focus();
    } else {
        if (selector.next() !== null) {
            ifNotTextPass(selector.next());
        }
    }
}

$(document).ready(function () {
    $('input[type="text"]').keypress(function (e) {
        if (e.keyCode === 13) {
            ifNotTextPass($(this));
        }
    });
});
도움이 되었습니까?

해결책 2

The problem is : an infinite call to the function , it's like and infinite loop.

i ve changed the whole code structure. And i ve arrived to the solution without getting any error.

This code will give the same behavior of the tab button to the enter button. plus it will focus just on text and textareas.

$(document).on('keydown', 'input[type="text"],textarea', function(e) {
            var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;

           if(key === 13) {
               e.preventDefault();
               $(this).focus();
               var inputs = $(this).parents('body').find(':input[type="text"]:enabled:visible:not("disabled"),textarea');

                inputs.eq( inputs.index(this)+ 1 ).focus();
                inputs.eq( inputs.index(this)+ 1 ).click();  

                e.preventDefault();
                e.stopPropagation();
            }   
}              

다른 팁

A jQuery selector will never return null, so you have recursion until the stack overflows. Try this instead:

if (selector.next().length) {
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top