Primefaces命令按钮按下回车键提交

5

我有两个命令按钮,当我按回车键时,第一个按钮会提交。我只想在用户按回车键时才提交第二个按钮。有什么建议吗?


如果可能的话,您可以将第一个按钮的类型更改为 type="button" 或将其变成 ajax 按钮(p:commandButton 默认为 ajax 按钮)。 - Matt Handy
2个回答

7

我知道这个问题已经很老了,但也许对于某些人来说,看到不同的解决方案仍然是有趣的。

您可以使用Primefaces defaultCommand,并为您喜欢的按钮添加一个id,以定义按下Enter键时发生的情况。就是这样。

<p:commandButton value="button1" action="#{bean.action1}"/>
<p:commandButton value="button2" id="btn_to_enter" action="#{bean.action2}"/>
<p:defaultCommand target="btn_to_enter" />

1

你需要交换这两个按钮的位置,仅此而已。

你当前的代码应该是:

<p:commandButton value="button1" action="#{bean.action1}"/>
<p:commandButton value="button2" action="#{bean.action2}"/>

默认情况下,将触发button1操作。您可以通过将style="float:right"添加到button1来向用户呈现替代视图。

<p:commandButton value="button1" style="float: right" action="#{bean.action2}"/>
<p:commandButton value="button2" action="#{bean.action1}"/>

使用上述代码,button1 将会在 button2 后面出现,并且每当按下 Enter 键时执行 button2 的操作。


嗨Selvin,我只在IE中遇到了这个问题,但是在http://www.primefaces.org/showcase/ui/dialogForm.jsf中可以看到。你知道除了定位之外还有其他方法吗? - c12

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接