У меня есть форма с большим количеством записей. Я хотел бы изменить свой фокус на следующее текстовое поле, после того как я ввел значение в текущее текстовое поле. и хочу продолжить этот процесс до последнего поля. Мой вопрос, это возможный моделировать то, что происходит при нажатии клавиши Tab через JavaScript, кодирующий, после того как я ввожу значение в текстовое поле.
Не нажимая клавишу Tab в клавиатуре, я хотел бы принести ту же функциональность через JavaScript. Действительно ли это возможно?
you just need to give focus to the next input field (by invoking focus()method on that input element), for example if you're using jQuery this code will simulate the tab key when enter is pressed:
var inputs = $(':input').keypress(function(e){
if (e.which == 13) {
e.preventDefault();
var nextInput = inputs.get(inputs.index(this) + 1);
if (nextInput) {
nextInput.focus();
}
}
});
function nextField(current){
for (i = 0; i < current.form.elements.length; i++){
if (current.form.elements[i].tabIndex == current.tabIndex+1){
current.form.elements[i].focus();
if (current.form.elements[i].type == "text"){
current.form.elements[i].select();
}
}
}
}
This, when supplied with the current field, will jump focus to the field with the next tab index. Usage would be as follows
<input type="text" onEvent="nextField(this);" />