输入按键的行为类似于Javascript中的Tab [英] Enter key press behaves like a Tab in Javascript

查看:130
本文介绍了输入按键的行为类似于Javascript中的Tab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个表单,其中按下回车键使焦点转到页面上的下一个表单元素。我在网上找到的解决方案是...

I'm looking to create a form where pressing the enter key causes focus to go to the "next" form element on the page. The solution I keep finding on the web is...

 <body onkeydown="if(event.keyCode==13){event.keyCode=9; return event.keyCode}">


$ b

所以这个问题的真正的原因是,如果有人知道一个解决方案,适用于FF和Chrome?此外,我宁愿不必将 onkeydown 事件添加到表单元素本身,但如果这是唯一的方法,它就必须这样做。

Unfortunately, that only seems to work in IE. So the real meat of this question is if anybody knows of a solution that works for FF and Chrome? Additionally, I'd rather not have to add onkeydown events to the form elements themselves, but if that's the only way, it will have to do.

此问题类似于问题905222 ,但应该在我看来是自己的问题。

This issue is similar to question 905222, but deserving of it's own question in my opinion.

编辑:我也看到人们提出这个问题,这不是好的风格,因为它偏离了用户习惯的表单行为。我同意!这是一个客户请求:(

also, I've seen people bring up the issue that this isn't good style, as it diverges from form behavior that users are used to. I agree! It's a client request :(

推荐答案

你可以通过编程来迭代form元素,添加onkeydown处理器,重用代码。

You could programatically iterate the form elements adding the onkeydown handler as you go. This way you can reuse the code.

这篇关于输入按键的行为类似于Javascript中的Tab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆