简体   繁体   English

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

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

I have write a and a in my page. 我在页面中写了一个和一个。 When enter key was pressed ,I want to do any js check, if success post an action. 当按下回车键时,我想做任何js检查,如果成功发布一个动作。

Javascript: Javascript:

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

HTML: 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>

when I write query("#search")[0].click() in the javascript.It can do the check, but can't do action="#{action.getResult}". 当我在javascript中编写query(“#search”)[0] .click()时。它可以执行检查,但不能执行action =“#{action.getResult}”。

I don't know how to do the losted action. 我不知道该怎么做。

The easiest way for you to submit your form from Javascript is to just invoke the click() event for the jQuery object of the <p:commandButton> . 从Java提交表单的最简单方法是只调用<p:commandButton>的jQuery对象的click()事件。

Most of the Primefaces components can be accessed from Javascript by declaring the widgetVar attribute. 可以通过声明widgetVar属性从Javascript访问大多数Primefaces组件。 This attribute creates a Javascript variable. 此属性创建一个Javascript变量。 Eg. 例如。

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

I can now access the variable in the formSubmit function. 我现在可以在formSubmit函数中访问变量。 This Javascript variable has an attribute called jq which references the underlying jQuery object and what you can use to invoke the click event. 此Javascript变量具有一个名为jq的属性,该属性引用基础jQuery对象以及可用于调用click事件的对象。

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

This will invoke the server side action of the commandButton and submit the form values if they pass validation. 如果它们通过验证,这将调用commandButton的服务器端action并提交表单值。

What kind of checks do you need? 您需要什么样的支票?

You don't need to manually submit on enter(especially when you have just one form). 您不需要在enter上手动提交(尤其是只有一种表单时)。 If you want to submit the form only if the user enters something you could take advantage of validateLength tag. 如果仅在用户输入内容时才想提交表单,则可以利用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" />

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

PS:What PF version do you use? PS:您使用什么PF版本?

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

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