简体   繁体   中英

how can I set the focus on inputText when I click reset button on Primefaces

The scenario is like this:

I have a form with an inputText and one button for reset the form. When I push the button, the form must be reset and the focus should point to the input text.

I am using primefaces 5.1. The codes go something like this:

<f:form id=myForm>
<h:panelGroup id="divInput" layout="block">
    <p:focus context="divInput"/>
    <p:inputText id="myInput"/>
    <p:commandButton type="reset"/>
</h:panelGroup>
</f:form>

I tried first with this oncomplete with no luck:

<p:commandButton type="reset" oncomplete="myInput.focus()"/>

The second time I tried with this script:

<script>
    //When DOM loaded we attach click event to button and set focus
    $(document).ready(function() {

        //attach click event to button
        $("#myForm [type='reset']").click(function(){
            $("#myForm [name='myInput']").focus();
        });


    });
</script>

Solved simply adding the event onclick with the right call to the method "focus":

<p:commandButton onclick="$('#myForm\\:myInput').focus();" type="reset"/>

Valid solution for RequestScoped bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM