使用 [Enter] 键且存在多个表单时提交预期 HTML 表单的通用解决方案?
最终的结果是在多表单页面上使用[Enter]时,在任何浏览器中都提交了预期受用户影响的表单。
我正在寻找一个通用解决方案,例如 jQuery 插件,它可以应用于多个存在此问题的网站,并使其立即生效以提交预期的表单。我不想编写条件语句或根据表单或网站更改它们。
可选更新:
对于更详细的场景,请考虑每个表单只有一个 type=Submit 按钮,这是默认/目标按钮 表单上的任何其他按钮都是 type=button,不考虑提交。
Based on this Stackoverflow question there seems to be no defined standard behaviour, or shared implementation across browsers, for hitting the [Enter] key in an HTML form field when multiple forms are present on the page. I'd like a jQuery solution that detects [Enter] on any form field, discovers its immediate enclosing form, and .click()'s that form's Submit button.
The end result is the form expected to be affected by the user is submitted in any browser when [Enter] is used on a multi-form page.
I'm looking for a generic solution, like a jQuery plugin, that can be applied to multiple websites having this problem and for it to immediately take effect to submit the expected form. I don't want to have to code conditional statements or alter them per form or website.
Optional Update:
For a more detailed scenario, consider each form has only one type=Submit button and that's the default/target button Any other buttons on the form are type=button and not considered for submission.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常用于检测 Enter:
在 objEvent 变量内部有一个 .target,您可以使用它来查看按下 Enter 的位置。
请参阅此处的小提琴: http://jsfiddle.net/maniator/svzeq/ (尝试在进入时按 Enter输入)
来自小提琴的代码:
The usual even for detecting enter:
Inside of the
objEvent
variable there is a.target
which you can use to see where the enter was pressed.See fiddle here: http://jsfiddle.net/maniator/svzeq/ (try pressing enter when in an input)
Code from fiddle:
我使用这个辅助函数来设置我的默认输入行为:
i use this helper function to set up my default enter behaviour:
我会做这样的事情:
当在文本类型的输入字段内按下返回键时,会提交相对表单(它不会单击提交按钮,但只会提交表单)
编辑 - 如果您想单击您可以执行的提交按钮:
I'd do something like this:
when the return key is pressed inside a input field of type text, the relative form is submitted (it doesn't click the submit button but it just submits the form)
EDIT - if you want to click the submit button you could do: