我在 javascript 中有以下代码,以便从文本框和文本区域进行导航。问题是该功能工作正常,但是当单击 Enter 时,它会导航到下一个文本框,同时它会导航到下一页,如何防止在单击 Enter 键时导航到下一页。有人可以帮我吗谢谢。
$('input[type="text"],textarea').keyup(function(e){
if(e.which==39 || e.which==13)
$(this).closest('td').next().find('input[type="text"],textarea').focus();
else if(e.which==37 || e.which==8)
$(this).closest('td').prev().find('input[type="text"],textarea').focus();
else if(e.which==40 || e.which==13)
$(this).closest('tr').next().find('td:eq('+$(this).closest('td').index()+')').find('input[type="text"],textarea').focus();
else if(e.which==38 || e.which==8)
$(this).closest('tr').prev().find('td:eq('+$(this).closest('td').index()+')').find('input[type="text"],textarea').focus();
});
最佳答案
在文本输入的 keyDown 上,捕获输入(如果是 Enter 键)并防止默认行为:
$('input[type="text"],textarea').keydown(function () {
if(event.keyCode == 13){
event.preventDefault();
}
});
如果我没记错的话,keyDown 是防止默认输入操作所必需的,而不是 keyUp。但两者都尝试一下,看看哪个有效。
关于javascript - 如何防止在 html 中使用 javascript 单击 Enter 键时移动到下一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253851/