在表单中按Enter键时要执行的默认操作 [英] Default action to execute when pressing enter in a form

查看:167
本文介绍了在表单中按Enter键时要执行的默认操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个按钮和几个输入字段的jsf 1.2表单.第一个按钮放弃输入的值,并使用db中的值重新填充页面,第二个按钮保存输入的值.当用户在光标位于输入字段之一中时按Enter键,提交表单并执行与第一个按钮关联的操作时,会发生问题.

I've got a jsf 1.2 form with two buttons and several input fields. The first button discards the entered values and repopulates the page with values from a db, the second button saves the entered values. The problem occurs when the user presses enter while the cursor is in one of the input fields, the form gets submitted and the action associated with the first button gets executed.

代码如下:

<h:commandButton action="#{bean.reset}" value="Reset" />
<h:commandButton action="#{bean.save}" value="Save" />

<!-- h:datatable with several h:inputText elements -->

是否可以在按Enter键时将特定按钮声明为默认操作?这种行为实际上是在某处指定的吗?

Is it possible to declare a specific button as the default action when pressing enter? Is this behaviour actually specified somewhere?

推荐答案

这不是JSF特有的.这是特定于HTML的. HTML5表单规范部分4.10.22.2 基本上指定了第一个与HTML DOM树中当前输入元素相同的<form>中以树顺序"出现的<input type="submit">元素将在回车时被调用.

This is not specific to JSF. This is specific to HTML. The HTML5 forms specification section 4.10.22.2 basically specifies that the first occuring <input type="submit"> element in the "tree order" in same <form> as the current input element in the HTML DOM tree will be invoked on enter press.

基本上有两种解决方法:

There are basically two workarounds:

  • 使用JavaScript捕获回车键并调用所需的按钮.

  • Use JavaScript to capture the enter key press and invoke the desired button.

<h:form onkeypress="if (event.keyCode == 13) { document.getElementById('formid:saveid').click(); return false; }">

如果表单中有textareas,则要将JS放在所有非textarea输入元素上,而不是在表单上.另请参见通过按Enter键来防止用户提交表单.

If you have textareas in the form, you'd like to put the JS on all non-textarea input elements instead of on the form. See also Prevent users from submitting a form by hitting Enter.

交换HTML中的按钮,然后使用CSS浮动将它们交换回来.

Swap the buttons in HTML and use CSS floats to swap them back.

<div style="width: 100px; clear: both;">
    <h:commandButton action="#{bean.save}" value="Save" style="float: right;" />
    <h:commandButton action="#{bean.reset}" value="Reset" style="float: left;" />
</div>

它可能只需要一些像素微调.当然,将CSS放在自己的.css文件中;使用style是不好的做法,上面的例子是为了简洁.

It may only require some pixel finetuning. Of course put CSS in its own .css file; using style is poor practice, the above example is for brevity.

如果碰巧使用PrimeFaces,则从3.2开始,您可以使用 <p:defaultCommand> 声明性地标识在按下表单中的Enter键时应调用的按钮.

If you happen to use PrimeFaces, since 3.2 you can use <p:defaultCommand> to declaratively identify the button which should be invoked when pressing enter key within the form.

<h:form>
    <p:defaultCommand target="save" />
    ...
    <h:commandButton id="reset" action="#{bean.reset}" value="Reset" />
    <h:commandButton id="save" action="#{bean.save}" value="Save" />
</h:form>

在使用JavaScript的情况下,该脚本将keydown侦听器附加到父<h:form>,后者反过来检查是否在非textarea/button/link元素中按下enter键,然后调用在目标元素上.基本上与该答案中第一个提到的解决方法相同.

It's under the covers using JavaScript for that which attaches a keydown listener to the parent <h:form> which in turn checks if the enter key is pressed in a non-textarea/button/link element, and then invokes click() on the target element. Basically the same as 1st mentioned workaround in this answer.

这篇关于在表单中按Enter键时要执行的默认操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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