简体   繁体   中英

populate textfield on selecting dropdown struts2

I have a text <s:select> in my jsp page .

Now what i have to do is when someone selects a value from this dropdown , i need to call my action class to get some value based on dropdown selection.

Now this value (which i got from my actionclass) should be shown in the <s:textfield> below this dropdown .

Please help !!

Well all you have to use the power of Ajax.You have multiple options to do this.

  1. User simple Javascript.
  2. User any javascript frameowrk like jquery,DOJO etc

Bind you code with on-click/change even of the Select tag and send a simple request to the S2 action.you can either use Stream result to send data back from the S2 action or better (in my opinion) send back JSON data from your action class and user Jquery build in functionality to parse the JSON data at JSP

user S2 JSON plugin to send and receive JSON data from Action and JSP to make life more easy.

Please follow this tutorial to know how to use JQuery with JSON and struts2

Update

You need to do something like this in your JSP code for Ajax and JQuery

var selectedState = document.getElementById("selectboxid");
        var statedata = selectedState.options[selectedState.selectedIndex].value;
        var formInput='state='+statedata;
$.getJSON('search/dropDownRenderer',formInput,function(data) {
}

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