如何跳转到下一个表单输入字段

How to jump to next form input field

本文关键字:输入 字段 表单 下一个 何跳转      更新时间:2023-09-26

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

如果我从下面的代码中删除"选择"标签,它可以工作。但我需要它与"选择"标签一起使用。我的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>
这是因为

现在元素的选项卡索引和数组索引是相同的,例如,第一个文本输入的tabIndex 为 1,是第二个输入字段,因此它在jumpform.elements数组中也有索引 1。如果删除select,它将成为第一个元素,因此下一个元素现在的索引为 1 并得到焦点。

试试next = elmnt.tabIndex + 1; .

我添加了

-事件侦听器

-标签索引+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>