javascript f:ajax 通过 onchange 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17806364/
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
f:ajax by onchange event
提问by Petr Du?ek
I have a special problem: I need to send a value to server by client onchange event without submiting the whole form. Is there some feature to do it?
我有一个特殊问题:我需要通过客户端 onchange 事件向服务器发送一个值,而无需提交整个表单。有什么功能可以做到吗?
I can handle the component by Javascript:
我可以通过 Javascript 处理组件:
<h:inputText onchange= ...js... >
And I can send a value by ajax:
我可以通过ajax发送一个值:
<f:ajax execute="name"/>
How to put it together?
如何把它放在一起?
It is solved, but I have another question:
它已经解决了,但我还有一个问题:
What is processed sooner - Ajax handling of the event or JavaScript handling?
什么是更快处理 - 事件的 Ajax 处理或 JavaScript 处理?
回答by skuntsel
Easy, AJAX was designed for partial submits / updates that happen on the page. You just need to specify event
attribute of <f:ajax>
tag and let it be change
, as you desire. As per partial form submit, specify ids of components to be updated on the server in tag's execute
attribute. But as default value for execute
of <f:ajax>
is exactly @this
(the component that fires the event), you can omit it altogether. Like so:
很简单,AJAX 是为页面上发生的部分提交/更新而设计的。您只需要指定标签的event
属性<f:ajax>
并change
随心所欲。根据部分表单提交,在标签的execute
属性中指定要在服务器上更新的组件的 id 。但由于execute
of 的默认值<f:ajax>
恰好是@this
(触发事件的组件),您可以完全省略它。像这样:
<h:inputText id="text" value="#{bean.text}">
<f:ajax event="change"/>
</h:inputText>
This way, after JavaScript change event happens, your bean model will be updated behind the scenes via AJAX.
这样,在 JavaScript 更改事件发生后,您的 bean 模型将通过 AJAX 在幕后更新。
As of which event happens first question you need to understand that it is the JavaScript event that triggers sending of AJAX request to the server, so, naturally, the latter happens first. Also, you can attach a client side callback to get a hook to JavaScript when AJAX response has been successfully committed, by specifying onevent
attribute.
至于哪个事件发生,第一个问题您需要了解,触发向服务器发送 AJAX 请求的是 JavaScript 事件,因此,自然地,后者首先发生。此外,通过指定onevent
属性,您可以附加客户端回调以在成功提交 AJAX 响应时获取 JavaScript 的挂钩。
回答by msmvc
My code:
我的代码:
<h:inputText id="appleNO" value="#{xxxModel.appleNO}" size="3" maxlength="3">
<f:ajax event="blur" execute="@form" listener="#{xxxController.xxxAction()}" render="appleNO"/>
</h:inputText>
when textbox lost foucs (onblur), xxxAction will be triggered.
当文本框丢失 foucs (onblur) 时,将触发 xxxAction。