我希望按Enter键时光标跳转到下一个输入字段。
如果我从下面的代码中删除“select”-tag,则可以正常工作。但我需要它与“选择”标签一起使用。我的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 :(得分:2)
那是因为现在元素的选项卡索引和数组索引是相同的,例如第一个文本输入的tabIndex为1,是第二个输入字段,因此它在jumpform.elements
数组中也有索引1。如果删除select
,它将成为第一个元素,因此下一个元素现在的索引为1并且会聚焦。
尝试next = elmnt.tabIndex + 1;
。
答案 1 :(得分: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>