如何在按下回车键时通过 JavaScript 调用 ap:commandButton?
我在我的页面中写了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Javascript 提交表单的最简单方法是仅调用
的 jQuery 对象的click()
事件。大多数 Primefaces 组件都可以通过声明
widgetVar
属性从 Javascript 访问。该属性创建一个 Javascript 变量。例如。我现在可以访问
formSubmit
函数中的变量。这个 Javascript 变量有一个名为jq
的属性,它引用底层 jQuery 对象以及可用于调用单击事件的内容。这将调用 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.I can now access the variable in the
formSubmit
function. This Javascript variable has an attribute calledjq
which references the underlying jQuery object and what you can use to invoke the click event.This will invoke the server side
action
of the commandButton and submit the form values if they pass validation.您需要什么类型的检查?
您不需要在输入时手动提交(特别是当您只有一张表单时)。
如果您只想在用户输入内容时提交表单,您可以利用
validateLength
标记。请参阅此处的演示: 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.See here a demo: http://www.primefaces.org/showcase-labs/ui/pprAjaxStatusScript.jsf
PS:What PF version do you use?