简体   繁体   中英

how to change the value of textfield + jQuery + Struts2

I have two textfield A , B : i want to do something like when i enter something in textfield A,this value will be use it in some action and result will be displayed in textfield B without clicking the submit button using ajax. how can i do it please ? note that i am a using struts2

Most of the information has already been provided by @alexanderb and i believe that Jquery is good way to go, now lets come to the second half of your question about using AJAX in your code. there are few ways you can send results from your action class.

  1. Return JSON from your action class and use above code.
  2. Use stream result type in your S2 code and place the data in the textfield.

Still i believe JSON with Jquery is good way to go which not only provides you the feasibility to easily extend functionality in future but also provide a clean way.Struts2 provides a plugin which can convert the data being send from your action class to JSON and all you will be left to parse the JSON data in your UI to fill the text-box.For details how to work with JSON in s2 refer to JSON plugin for detail

With JSON plugin your flow will be

  • Call your Action class on specific event in text box.
  • Configure your action to return JSON data using S2-JSon plugin.
  • Action will return JSON to the Jquery code.
  • Parse the JSON data and fill the text box with the value

It should be easy. Suppose you have action that takes value as string and return some string back, availble on '/app/service' url.

You can create such code for that:

$(function() {

  $('#text_1').on('keyup', function() {

     var value = $(this).val();
     $.post('/app/service', JSON.stringify(value), function (r) {
       $('text_1').text(r);
     }); 

  });

});

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