Restore text value after validation fail

2019-07-31 02:23发布

I am using jQuery to bind a keyup event of an inputText, so that the value of it will be set to the span text.

<h:form id="formId">
     <script type="text/javascript">
      $(document).ready(function(){
        var portletNamespace = '#<portlet:namespace/>\\:formId\\:';             
        $(portletNamespace+'txtSimpleText').live('keyup',function(){    
             $(portletNamespace+'spanText').text($(portletNamespace+'txtSimpleText').val());    
        }); 
     });    
    </script>

    <h:inputText id="txtSimpleText" value=""/>
    <h:inputText required="true" value=""/>
    <h:outputText id="spanText"></h:outputText >
    <h:commandButton actionListener="#{myController.someAction}" value="do somthing"/>
</h:form>

It works fine, but at the first validation fail I lose my spanText text! This only happens the first time; at the second validation fail the spanText text is still there. I only need it in the view layer, and have no need to use it after the form is submitted. Please show me how to restore my spanText after validation fail!

PS: There is a reason I cannot use Ajax to store and set the value for spanText.

0条回答
登录 后发表回答