[英]Specifying default a4j:commandButton when press Enter in JSF form
My JSF form contain 我的JSF表单包含
Multiple h:inputText
and Multiple a4j:commandButton
多个 h:inputText
和多个 a4j:commandButton
Also 也
I have 2 buttons (search) and (reset) 我有2个按钮(搜索)和(重置)
My need when i press Enter
during editing any h:inputText
我在编辑任何h:inputText
时press Enter
需要
(search) button will be fired (搜索)按钮将被触发
<h:form id="searchForm" onkeypress="formKeypress();">
<h:panelGrid columns="4" >
<h:outputText value="name" />
<h:panelGroup>
<h:inputText
title="name"
value="#{myController.name}" >
</h:inputText>
<a4j:commandButton
title="name Detail"
action="#{myController.nameDetail}" >
</a4j:commandButton>
</h:panelGroup>
<h:outputText value="city" />
<h:panelGroup>
<h:inputText
title="city"
value="#{myController.city}" >
</h:inputText>
<a4j:commandButton
title="name Detail"
action="#{myController.cityDetail}" >
</a4j:commandButton>
</h:panelGroup>
</h:panelGrid>
<h:panelGrid >
<h:panelGroup>
<a4j:commandButton
id="searchButton"
value="search" title="search"
action="#{myController.search}"
render="searchResultsGrid" />
</h:panelGroup>
</h:panelGrid>
<f:verbatim>
<script type="text/javascript" >
<!--
function formKeypress() {
// if (event.keyCode == 13) {
// document.getElementById('searchForm:searchButton').click();
// return false;
// }
}
-->
</script>
</f:verbatim>
</h:form>
the problem is that when i press Enter
during editing any h:inputText
the first a4j:commandButton
in the form will be fired Not the (search) button 问题是,当我在编辑任何h:inputText
的过程中press Enter
时,将触发表单中的第一个a4j:commandButton
而不是(search)按钮
Note 注意
I checked the answer 我检查了答案
Default action to execute when pressing enter in a form 在表单中按Enter键时执行的默认操作
But that for h:commandButton
但是对于h:commandButton
And I face JavaScript error exception 而且我遇到了JavaScript错误异常
ReferenceError: event is not defined
[Break On This Error]
if (event.keyCode == 13) {
You have to pass the event to the JavaScript function: 您必须将事件传递给JavaScript函数:
<h:form onkeypress="return formKeypress(event);"
id="srch">
<h:inputText />
<h:commandButton id="no" value="NO!" action="#{bean.no}" />
<h:commandButton id="yes" value="YES!" action="#{bean.yes}" />
</h:form>
<script type="text/javascript">
var formKeypress = function(event) {
if (event.keyCode === 13) {
document.getElementById('srch:yes').click();
return false;
}
return true;
};
</script>
Use the PrimeFaces component: 使用PrimeFaces组件:
<!-- Default button when pressing enter -->
<p:defaultCommand target="submit"/>
Use this in combination with a focus component and you will rock! 将此功能与聚焦组件结合使用,您将大放异彩!
<!-- Focus on first field, or first field with error -->
<p:focus context="feesboek"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.