问题
I have a simple script that auto-advances the cursor to the next field in my html form after one character is entered...and it works great for that. This is the simple code:
function autotab(current,to)
{
if (current.getAttribute && current.value.length==current.getAttribute("maxlength"))
{
to.focus()
}
}
Then of course I use the onkeyup to advance it, like this:
<input onkeyup="autotab(this, document.jumble.w1b)" type="text">
Again, that works great. However, when I do Shift+Tab to go one box back, it auto-advances before I can type anything.
Does anyone have a code example of how to make Shift+Tab work? I have tried different variations of trying to use the keycodes to detect shift and tab, but it doesn't seem to work.
Any help would be greatly appreciated!
回答1:
You should prevent to.focus in case of shift + tab jsfiddle
function autotab(event, current,to) {
event = event || window.event;
if(event.keyCode < 65 || event.keyCode > 90){
return ;
}
if (current.getAttribute && current.value.length==current.getAttribute("maxlength"))
{
to.focus()
}
}
EDIT: Add event as first parameter in autotab: <input onkeyup="autotab(event, this, document.jumble.w1b)" type="text">
来源:https://stackoverflow.com/questions/12642774/auto-advance-in-form-fields