Onchange event in Struts2
22,853
There is no difference in how you apply onchange
or any other javascript event handler to struts tag as compared to regular HTML tags. Struts2 select Tag Ref
<s:select name="menuItem" list="menuItems" listKey="menuItemID"
listValue="menuItemName" headerValue="--MenuItems--"
cssClass="selectbox_bg2" id="select" onchange="handleChange(this.value)"/>
JavaScript
function handleChange(value){
window.location="callMyAction?ValueToSubmit="+value; //or you can submit a form from here or make an ajax call
}
Or if you are using jquery then
$("#select").change(function(e){
var value = $(this).val();
//submit a form or make ajax call or use window.location
});
Comments
-
jose over 3 years
How to call action on onchange event with select option in struts2.
Here is my code and how can I integrate with On change event.
<s:select name="menuItem" list="menuItems" listKey="menuItemID" listValue="menuItemName" headerValue="--MenuItems--" cssClass="selectbox_bg2" id="select" />
Can anyone please provide an example..
Thanks,
-
jose over 11 yearsOK Thank you..so how we execute an action for that onChange event?
-
Anupam over 11 yearsSee the edited answer. you can submit a form or make an ajax call
-
jose over 11 yearsOk it's fine..And I have one more doubt how we get this parameter(value) in a struts2 action class??
-
Anupam over 11 yearsFor that you need to declare a variable with the same name as in jsp e.g. in
handleChange()
function I am assigning the value toValueToSubmit
variable, so incallMyAction
action class you need to declare a variable with nameValueToSubmit
and also its getter/setter -
Thor Hovden almost 11 yearsNice answer. In my situation I have several <s:select>. How do you write the jquery handler in this situation? I guess you need to include the select id somehow.
-
Anupam almost 11 years@ThorHovden If your behaviour for all the
select
elements is same then you can assign a class to these elements usingcssClass="myClass"
and then write a common handler using$(".myClass")
. If the behavior needs to be different then yes, you have to define separate handlers