Question

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?

Was it helpful?

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>

OTHER TIPS

The before is running after the form is serialized, so the old value is used. You need to modify code like this

<script type="text/javascript">
  $(document).ready(function(){
   $('#before').click(function() {
       $('#text').val('Text Changed by jQuery');
   });
  });
</script>
<s:div id="result"/>
<s:form id="form2" action="textchange" method="POST">
<s:textfield id="text" name="text" value="Hello World!!!"/>
<sj:submit  id="before" targets="result" />
</s:form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top