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
});
Share:
22,853
jose
Author by

jose

Looking Up....

Updated on September 30, 2020

Comments

  • jose
    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
    jose over 11 years
    OK Thank you..so how we execute an action for that onChange event?
  • Anupam
    Anupam over 11 years
    See the edited answer. you can submit a form or make an ajax call
  • jose
    jose over 11 years
    Ok it's fine..And I have one more doubt how we get this parameter(value) in a struts2 action class??
  • Anupam
    Anupam over 11 years
    For that you need to declare a variable with the same name as in jsp e.g. in handleChange() function I am assigning the value to ValueToSubmit variable, so in callMyAction action class you need to declare a variable with name ValueToSubmit and also its getter/setter
  • Thor Hovden
    Thor Hovden almost 11 years
    Nice 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
    Anupam almost 11 years
    @ThorHovden If your behaviour for all the select elements is same then you can assign a class to these elements using cssClass="myClass" and then write a common handler using $(".myClass"). If the behavior needs to be different then yes, you have to define separate handlers