javajqueryjspstruts2struts2-jquery-plugin

Dynamic input is not recognized in struts2-jquery tags


In my application i need to change the content of the <s:textfield> dynamically my code is given below:

<script type="text/javascript">
    $.subscribe('before', function(event, data) {                
        $('#text').val('Text Changed by jQuery');
    });               
</script>       

<s:form id="form2" action="textchange" >        
    <s:textfield id="text" name="text" value="Hello World!!!"/>                                                        
    <sj:submit  targets="result" onBeforeTopics="before" />
</s:form>                
   

my expecting output is

Text Changed by jQuery

but I'm getting

Hello World!!!

I'm using Struts2-jQuery-plugin 3.5.1. How to get the dynamic output?


Solution

  • NOTE: This is not the best way to do that.

    But remove subscribe and onBeforeTopics attribute. Add id to submit button and bind click event to it.

    <script type="text/javascript">
      $(function(){
        $("#form2Submit").click(function() {   
          $('#text').val('Text Changed by jQuery');
        });
      });
    </script>
    
    <s:form id="form2" action="textchange">        
      <s:textfield id="text" name="text" value="Hello World!!!" />                                                        
      <sj:submit id="form2Submit" targets="result" />
    </s:form>
    

    Any other way. Using <sj:a> tag with subscribe.

    <script type="text/javascript">
      $(function(){
        $.subscribe('before', function(event, data) {        
          $('#text').val('Text Changed by jQuery');
        }); 
      });
    </script>
    
    <s:form id="form2" action="textchange">        
      <s:textfield id="text" name="text" value="Hello World!!!" />                                                        
      <sj:a targets="result" onClickTopics="before" formIds="form2" button="true">
        Submit
      </sj:a>
    </s:form>