Auto-advance in form fields

霸气de小男生 提交于 2019-12-11 06:46:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!