如何在按下回车键时通过 JavaScript 调用 ap:commandButton?

发布于 2024-12-21 17:01:01 字数 595 浏览 2 评论 0原文

我在我的页面中写了 a 和 a 。 当按下回车键时,我想做任何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}"。

我不知道如何做丢失的动作。

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.

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>

when I write query("#search")[0].click() in the javascript.It can do the check, but can't do action="#{action.getResult}".

I don't know how to do the losted action.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧笙踏歌 2024-12-28 17:01:01

从 Javascript 提交表单的最简单方法是仅调用 的 jQuery 对象的 click() 事件。

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

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

我现在可以访问 formSubmit 函数中的变量。这个 Javascript 变量有一个名为 jq 的属性,它引用底层 jQuery 对象以及可用于调用单击事件的内容。

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

这将调用 commandButton 的服务器端 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>.

Most of the Primefaces components can be accessed from Javascript by declaring the widgetVar attribute. This attribute creates a Javascript variable. 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. This Javascript variable has an attribute called jq which references the underlying jQuery object and what you can use to invoke the click event.

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.

网名女生简单气质 2024-12-28 17:01:01

您需要什么类型的检查?

您不需要在输入时手动提交(特别是当您只有一张表单时)。
如果您只想在用户输入内容时提交表单,您可以利用 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版本?

What kind of checks do you need?

You don't need to manually submit on enter(especially when you have just one form).
If you want to submit the form only if the user enters something you could take advantage of validateLength tag.

<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

PS:What PF version do you use?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文