我在网上找到的答案都没有用。
当我的页面加载时,我让我的 JS 文件选择第一个文本字段;哪个有效。 然后我想要它,这样如果在文本字段中按下回车键,它就会继续到下一个文本字段。现在我有...
$('input[type="text"]').keyup(function(e) {
if(e.keyCode == 13) {
$(this).next().focus();
}
});
这是行不通的。
我的输入是...
<input type="text" name="username" id="username" autocomplete="off" />
<input type="text" name="password" id="password" autocomplete="off" />
如果我做了一些愚蠢的事情,我深表歉意,但我已经尝试解决这个问题太久了。
最佳答案
您只需将您的代码片段指向文档就绪函数。如下图
$(document).ready(function (){
$('input[type="text"]').keyup(function(e) {
var key_code = e.keyCode || e.which;
if(key_code == 13)
{
$(this).nextAll('input[type="text"]').first().focus();
}
});
});
如果在第二个文本输入字段之前有任何其他 DOM,请注意建议使用 nextAll('input[type="text"]').first().focus()
。
关于javascript - 按回车键切换文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39141844/