Can h: inputText call a method inside a managed component when I press the return / enter key

So, I have inputTextone that has a value hook myBean.text, and I want that if I press the enter / return key, inputText will call the method inside myBeanto do something. Can someone help me?

+6
source share
6 answers

According to your background, I know that you are using JSF 2.0, so here is the JSF 2.0 answer given here: use <f:ajax>that listens for the event changeand uses the event keypressto trigger it when you enter the key (key code 13).

<h:inputText value="#{bean.text1}" 
    onkeypress="if (event.keyCode == 13) { onchange(); return false; }">
    <f:ajax event="change" listener="#{bean.listener}" />
</h:inputText>

#{bean.listener} must point to a type method

public void listener(AjaxBehaviorEvent event) {
    // ...
}
+14

- inputText commandButton .

:

<h:form>
  <h:inputText styleClass="myText" value="#{myBean.text}"/>
  <h:commandButton styleClass="myButton" action="#{myBean.myMethod}" style="display:none;" value="submit"/>
</h:form>

UPDATE

Seam, <s:defaultAction/>. , commandButton , ENTER.

<h:commandButton class="myButton" action="#{myBean.myMethod}" style="display:none;" value="submit">
  <s:defaultAction/>
</h:commandButton>

Seam,

Javascript; jQuery. :

$('input.myText').keypress(function(e) {
    if (e.which == 13) {
        $('.myButton').click();
    }
});
+3

:

<h:inputText value="#{bean.text1}" 
     onkeypress="return (event.keyCode == 13)">
  <f:ajax event="keypress" listener="#{bean.listener}" />
</h:inputText>

JSF , , "onkeypress", f: ajax. , false, . f: ajax, , , , , , .

+3

- :

<h:inputText value="123" onkeyup="if(event.keyCode==13)this.blur();" onchange="document.getElementById('fakebutton').click();" valueChangeListener="#{yourbean.dosomething}"/>
<h:commandButton id="fakebutton"actionListener="#{yourbean.fakeaction}"/>

bean:

public void dosomething(ValueChangeEvent event)
{
    System.out.println("I did something");
}
public void fakeaction(ActionEvent event)
{
    System.out.println("I do no nothing");
}
+2

, , , Damo - , ( ;-). ( ). , ajax , enter . , enter , ajax ( ). , - . .

<div class="boxWrapper searchWrapper right">
    <div class="boxHeader"><div class="tail" /></div>
    <div class="boxContent">
        <h:form prependId="false">
            <label>SEARCH:</label>
            <div class="searchBox">
                <f:ajax execute="@form" render="@none">
                    <h:inputText id="keywords" value="#{search.searchString}" onblur="inputTextBlur( this, '#{search.searchString}' );" onfocus="inputTextFocus( this, '#{search.searchString}' );" />
                    <h:commandButton action="#{search.search}" style="display:none;" value="submit" />
                    <h:commandLink id="cmdLink" action="#{search.search}">
                        <!-- the span here is to apply .searchWrapper span { } specific css to the image position -->
                        <span><h:graphicImage styleClass="iconSearch" url="images/iconMask.png" width="17" height="17" /></span>
                    </h:commandLink>
                </f:ajax>
            </div>
        </h:form>
    </div>
    <div class="boxFooter"><div class="tail" />
</div>

p.s. - @BalusC: , 100% , , , . , . - , , .

,

+1

, , , .

, f:ajax . BalusC.

this. onchange().

<h:inputText value="#{bean.text1}" 
    onkeypress="if (event.keyCode == 13) { this.onchange(); return false; }">
    <f:ajax event="change" listener="#{bean.listener}" />
</h:inputText>
+1

All Articles