繁体   English   中英

如何在输入keypress时通过JavaScript调用ap:commandButton?

[英]How to invoke a p:commandButton by JavaScript on enter keypress?

我在页面中写了一个和一个。 当按下回车键时,我想做任何js检查,如果成功发布一个动作。

Javascript:

function text_onkeypress() {
    if(event.keyCode==13){
        formSubmit();
    }
}

HTML:

<p:inputText id="context" value="#{bean.fileName}"
 onkeydown="text_onkeypress();"></p:inputText>
<p:commandButton id="search" value="submit" onclick="return formSubmit();" 
 action="#{action.getResult}" ></p:commandButton>

当我在javascript中编写query(“#search”)[0] .click()时。它可以执行检查,但不能执行action =“#{action.getResult}”。

我不知道该怎么做。

从Java提交表单的最简单方法是只调用<p:commandButton>的jQuery对象的click()事件。

可以通过声明widgetVar属性从Javascript访问大多数Primefaces组件。 此属性创建一个Javascript变量。 例如。

<p:commandButton id="search" value="submit" onclick="return formSubmit();" action="#{action.getResult}" widgetVar="searchClientVar" />

我现在可以在formSubmit函数中访问变量。 此Javascript变量具有一个名为jq的属性,该属性引用基础jQuery对象以及可用于调用click事件的对象。

function text_onkeypress() {
  if(event.keyCode==13) {
    searchClientVar.jq.click();
  }
}

如果它们通过验证,这将调用commandButton的服务器端action并提交表单值。

您需要什么样的支票?

您不需要在enter上手动提交(尤其是只有一种表单时)。 如果仅在用户输入内容时才想提交表单,则可以利用validateLength标记。

<h:outputLabel for="firstname" value="Firstname: *" />  
        <p:inputText id="firstname"   
                value="#{personBean.firstname}"   
                required="true" requiredMessage="You have to enter your name" label="Firstname">  
            <f:validateLength minimum="2" />  
        </p:inputText>  
        <p:message for="firstname" />

观看此处的演示: http : //www.primefaces.org/showcase-labs/ui/pprAjaxStatusScript.jsf

PS:您使用什么PF版本?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM