如何在输入按键时通过 JavaScript 调用 ap:commandButton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8517884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to invoke a p:commandButton by JavaScript on enter keypress?
提问by wsxiapiaoxue
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.
我不知道如何做丢失的动作。
采纳答案by spauny
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.
您不需要在输入时手动提交(尤其是当您只有一个表单时)。如果您只想在用户输入内容时提交表单,您可以利用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版本?
回答by maple_shaft
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>
.
从 Javascript 提交表单的最简单方法是调用 .js 文件click()
的 jQuery 对象的事件<p:commandButton>
。
Most of the Primefaces components can be accessed from Javascript by declaring the widgetVar
attribute. This attribute creates a Javascript variable. Eg.
大多数 Primefaces 组件都可以通过声明widgetVar
属性来从 Javascript 访问。此属性创建一个 Javascript 变量。例如。
<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.
我现在可以访问formSubmit
函数中的变量。这个 Javascript 变量有一个名为的属性jq
,它引用底层 jQuery 对象以及您可以用来调用单击事件的内容。
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.
这将调用action
commandButton的服务器端并在它们通过验证时提交表单值。