Если имеются несколько полей, в которые вводятся цифры, то может возникнуть необходимость автоматического перевода фокуса и курсора на следующее поле. В этой статье сниппеты на jQuery и JavaScript.
Цифра:
$('input').keyup(function(){
if($(this).val().match(/^\d{1}$/)){
$(this).next('input').focus();
}else{
$(this).val('');
}
});Число:
$('input').keyup(function(e){
var symbol = String.fromCharCode(e.keyCode);
if($(this).val().match(/^\d+$/) && !symbol.match(/^\d{1}$/)){
$(this).val(parseInt($(this).val()));
$(this).next('input').focus();
}else{
$(this).val('');
}
});или:
JavaScript:
function testJump(x){
var ml = ~~x.getAttribute('maxlength');
if(ml && x.value.length >= ml){
do{
x = x.nextSibling;
}
while(x && !(/text/.test(x.type)));
if(x && /text/.test(x.type)){
x.focus();
}
}
}HTML:
<div>
<input type="text" onkeyup="testJump(this);" maxlength="5" size="5">
<input type="text" onkeyup="testJump(this);" maxlength="4" size="4">
<input type="text" onkeyup="testJump(this);" maxlength="7" size="7">
<input type="text" onkeyup="testJump(this);" maxlength="5" size="5">
</div>