Javascript 如何将光标移动到下一个文本字段?

Javascript 如何将光标移动到下一个文本字段?,javascript,jquery,Javascript,Jquery,在通过的工作中,我尝试通过按enter键将焦点从一个文本字段移动到另一个文本字段。我在以下链接中找到了可能的答案: 但这些都不适合我。我的代码: <script> $('input[type='text']').keyup(function(e) { if(e.keyCode == 13) { $(this).next().focus(); } }); </script> <input type='text' /> <

在通过的工作中,我尝试通过按enter键将焦点从一个文本字段移动到另一个文本字段。我在以下链接中找到了可能的答案:


但这些都不适合我。我的代码:

<script>
$('input[type='text']').keyup(function(e) {
    if(e.keyCode == 13) {
        $(this).next().focus();
    }
});
</script>
<input type='text' />
<input type='text' />
<input type='text' />
<input type='text' />
<input type='text' />
<input type='text' />

$('input[type='text']).keyup(函数(e){
如果(e.keyCode==13){
$(this.next().focus();
}
});
只需将
$('input[type='text'])
更新为
$('input[type='text'])
。你有未闭合的字符串

工作代码笔。

只需将
$('input[type='text'])
更新为
$('input[type='text'])
。你有未闭合的字符串

工作代码笔。

$(文档).ready(函数(){
$('form')。在('submit',函数(事件){
event.preventDefault();
})
$('input[type=“text”]”)。键控(函数(事件){
如果(event.keyCode===13){
$(this.next().focus();
}
});
})

$(文档).ready(函数(){
$('form')。在('submit',函数(事件){
event.preventDefault();
})
$('input[type=“text”]”)。键控(函数(事件){
如果(event.keyCode===13){
$(this.next().focus();
}
});
})

以下是发生的情况:

<!-- Start loading the page -->
<script>
// execute javascript. Note that there
// are no inputs on the page currently - they will be loaded later

// This selector matches nothing:
$("input[type='text']").keyup(function(e) {
    if(e.keyCode == 13) {
        $(this).next().focus();
    }
});
</script>
<!-- JavaScript executed. Now add inputs to the page -->
<input type='text' />
<input type='text' />

下面是正在发生的事情:

<!-- Start loading the page -->
<script>
// execute javascript. Note that there
// are no inputs on the page currently - they will be loaded later

// This selector matches nothing:
$("input[type='text']").keyup(function(e) {
    if(e.keyCode == 13) {
        $(this).next().focus();
    }
});
</script>
<!-- JavaScript executed. Now add inputs to the page -->
<input type='text' />
<input type='text' />


关于您的实际问题-它与服务器端无关。您所做的完全是客户端的,并且通过将脚本标记移动到输入下面来修复。为什么要使用enter?事实上,没有人会期望这种行为。@Waxi你是对的。使用Enter进入下一个输入将是无用的,因为没有人会使用它。切换输入焦点的默认键是Tab键,该键不带javascript@Al.G. : 我已经根据你的建议更新了代码。但它仍然不起作用。@DragonBall您是否使用我或其他人的答案解决了您的问题?你没有回复我最后的评论。关于你的实际问题-它与服务器端无关。您所做的完全是客户端的,并且通过将脚本标记移动到输入下面来修复。为什么要使用enter?事实上,没有人会期望这种行为。@Waxi你是对的。使用Enter进入下一个输入将是无用的,因为没有人会使用它。切换输入焦点的默认键是Tab键,该键不带javascript@Al.G. : 我已经根据你的建议更新了代码。但它仍然不起作用。@DragonBall您是否使用我或其他人的答案解决了您的问题?您没有回复我的上一条评论。因为jQuery未包含在内。代码本身看起来很好。修复了!感谢您指出这一点——有人能帮我一下吗,如何在代码片段中包含jquery?现在有一个难题:因为jQuery不包括在内。代码本身看起来很好。修复了!感谢您指出这一点——有人能帮我一下吗,如何在代码片段中包含jquery?现在有一个难题:实际上我正在php中的变量$var中编写代码。然后echo$var。这就是我使用$('input[type='text'])的原因。否则,它会在/opt/lampp/htdocs/1.php行中显示错误、解析错误:语法错误、意外的“文本”(T_字符串)4@DragonBall但是在您删除php部分之后,Nicolae的解决方案应该可以工作了?@Al.G。这一个也不起作用,实际上我在php中的变量$var中编写代码。然后echo$var。这就是我使用$('input[type='text'])的原因。否则,它会在/opt/lampp/htdocs/1.php行中显示错误、解析错误:语法错误、意外的“文本”(T_字符串)4@DragonBall但是在您删除php部分之后,Nicolae的解决方案应该可以工作了?@Al.G。而且这个也不起作用,@DragonBall抱歉,我忘了修正引号。现在应该可以了。@AI.G。我现在添加一个图像。它不起作用了,@DragonBall抱歉,我忘了修正报价。现在应该可以了。@AI.G。我现在添加一个图像。它不起作用了,