i need to do an ajax request when i click "enter" in inputText field, without refresh page.
That's my code:
<h:form id="form-test">
<p:growl id="messages" showDetail="true"/>
<p:inputText id="inputEnter"
onkeypress="if (event.keyCode == 13) {onchange(); return false; }"
value="#{bean.content}">
<f:ajax event="change" listener="#{bean.save}" update="messages"/>
</p:inputText>
</h:form>
And in bean I have a simple method:
public void save(AjaxBehaviorEvent event){
...
}
All works, but It refresh page !!! Why ? How can I solve it ?
There is already a thread with a question somehow like yours How to handle the ENTER keypressed to trigger an onBlur() event?
I would just change
onkeypress="if (event.keyCode == 13) {e.preventDefault();
inputEnter.onchange(); return false; }"
If you use JQuery
$('#form-test\\:inputEnter').onchange();
I think you need to enter the render param , so it does not render the whole page again.
<h:form id="form-test">
<p:growl id="messages" showDetail="true"/>
<p:inputText id="inputEnter"
onkeypress="if (event.keyCode == 13) {onchange(); return false; }"
value="#{bean.content}">
<f:ajax event="change" render="messages" listener="#{bean.save}" update="messages"/>
</p:inputText>
</h:form>