Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0: ajax request when press ENTER

Tags:

ajax

jsf

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 ?

like image 342
enfix Avatar asked May 08 '12 22:05

enfix


1 Answers

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();
like image 182
Oscar Castiblanco Avatar answered Oct 19 '22 13:10

Oscar Castiblanco