2014-12-28 119 views
2

我希望光标在按下回车键时跳转到下一个输入字段。如何跳转到下一个表单输入字段

如果我从下面的代码中删除“选择”标签,它会起作用。但我需要它与“选择”标签一起工作。我的JavaScript有什么问题?

<html> 
<head> 
<script type="text/javascript"> 
function jump(elmnt,content) 
{ 
if (event.keyCode == 13) 
    { 
    next=elmnt.tabIndex 
    document.jumpForm.elements[next].focus() 
    } 
} 
</script> 
</head> 
<body> 
<p>This script automatically jumps to the next input field when the current field's maxlength has been reached.</p> 
<form name="jumpForm"> 
<select style = "border: 4px solid red;" name = "typ" tabindex="0"> 
         <option value = "+">Zugang</option> 
         <option value = "-">Abgang</option> 
         <option value = "b">Bruchware</option> 
         <option value = "x">Lagerort löschen</option> 
        </select> 
<input size="3" tabindex="1" onkeyup="jump(this,this.value)"> 
<input size="3" tabindex="2" onkeyup="jump(this,this.value)"> 
<input size="3" tabindex="3" onkeyup="jump(this,this.value)"> 
</form> 
</body> 
</html> 

回答

0

我增加

-eventlistener

-tabindex + 1

-onkeyup选择

<html> 
    <head> 
    <script type="text/javascript"> 
    function jump(elmnt,content) 
    { 
    document.addEventListener('keyup', function(event) { 

    if (event.keyCode == 13) 
     { 

     next=elmnt.tabIndex+1; 
     document.jumpForm.elements[next].focus() 
     } 

    }); 
    } 
    </script> 
    </head> 
    <body> 
    <p>This script automatically jumps to the next input field when the current field's maxlength has been reached.</p> 
    <form name="jumpForm"> 
    <select style = "border: 4px solid red;" name = "typ" tabindex="0" onkeyup="jump(this,this.value)"> 
          <option value = "+">Zugang</option> 
          <option value = "-">Abgang</option> 
          <option value = "b">Bruchware</option> 
          <option value = "x">Lagerort löschen</option> 
         </select> 
    <input size="3" tabindex="1" onkeyup="jump(this,this.value)"> 
    <input size="3" tabindex="2" onkeyup="jump(this,this.value)"> 
    <input size="3" tabindex="3" onkeyup="jump(this,this.value)"> 
    </form> 
    </body> 
    </html> 
2

这是因为现在元素的标签索引和数组索引是相同的,例如,第一个文本输入的tabIndex为1,并且是第二个输入字段,因此它在jumpform.elements阵列中也具有索引1。如果您删除select,它将成为第一个元素,因此下一个元素现在具有索引1并且变得有重点。

尝试next = elmnt.tabIndex + 1;

相关问题