Вопрос

for example I've 3 elements in my HTML code for text input.

<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="text" name="txt3" />

At first load, I set by js to focus in txt1. My question is, How I can manipulate keyascii when I pressing tab from txt1 to txt3 ?

The fact, I've added some js code with jquery to do that, but doesn't work! It's always focused to txt2. This is my js code:

$('input[name="txt1"]').keyup(function(e){
        if((e.keyWhich || e.keyCode) == 9){
            $('input[name="txt3"]').focus();
        }
    });
Это было полезно?

Решение

Listen for keydown and use e.preventDefault() to prevent the default behaviour. The default behaviour for pressing tab is executed before the keyup event is being fired. That's why you have to use keydown instead.

$('input[name="txt1"]').keydown(function(e){
    if((e.keyWhich || e.keyCode) == 9){            
        e.preventDefault();
        $('input[name="txt3"]').focus();
    }
});

See this Fiddle

Другие советы

You should listen for keydown instead and be using e.preventDefault()

Example:

$('input[name="txt1"]').keydown(function(e){
    if((e.keyWhich || e.keyCode) == 9) {
        e.preventDefault();
        $('input[name="txt3"]').focus();
    }
});

See on JSFiddle

Documentation of preventDefault

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top