Question

I have a JSP page and in that I am using Struts2 tags for making my registration form. Now the first field is type which uses s:select tag. Now, depending on what is selected by the user I want to load next texfields on the same page below the select box. How can I do it? Here is my approach but it's not working.

<s:form action="clientAction" method="post">
  <s:select name="type" label="Registration Type" 
    list="{'Student','Faculty'}" emptyOption="false" />
  <s:if test="type.equals(Student)">        
    <s:textfield label="FirstName" name="s_fname"/>
    <s:textfield label="LastName" name="s_lname"/>
    <s:textfield label="UserName" name="s_username"/>
    <s:password label="Password" name="s_password"/>
    <s:textfield label="EmailId" name="s_email_id"/>
  </s:if>       
  <s:if test="type.equals(Faculty)">
    <s:textfield label="FirstName" name="fac_fname"/>
    <s:textfield label="LastName" name="fac_lname"/>
    <s:textfield label="UserName" name="fac_username"/>
    <s:password label="Password" name="fac_password"/>
    <s:textfield label="EmailId" name="fac_email"/>
    <s:textfield label="Course Name" name="course_name"/>
    <s:textfield label="Course Id" name="course_id"/>
  </s:if>
  <s:submit/>
</s:form>
Was it helpful?

Solution

You could do it with JQuery, and you don't need to load fields from the server, as well as mess up JavaScript code with JSP code. You should use css_xhtml theme to hide/show fields on change event of select box. Like in this example

<s:form action="clientAction" method="post" theme="css_xhtml">
    <s:select id="RegistrationType" name="type" label="Registration Type"
              list="{'Student', 'Faculty'}" emptyOption="false" />
  <s:div id="Student" cssStyle="display: none;">
    <s:textfield label="FirstName" name="s_fname"/>
    <s:textfield label="LastName" name="s_lname"/>
    <s:textfield label="UserName" name="s_username"/>
    <s:password label="Password" name="s_password"/>
    <s:textfield label="EmailId" name="s_email_id"/>
    <s:submit/>
  </s:div>

  <s:div id="Faculty" cssStyle="display: none;" >
    <s:textfield label="FirstName" name="fac_fname"/>
    <s:textfield label="LastName" name="fac_lname"/>
    <s:textfield label="UserName" name="fac_username"/>
    <s:password label="Password" name="fac_password"/>
    <s:textfield label="EmailId" name="fac_email"/>
    <s:textfield label="Course Name" name="course_name"/>
    <s:textfield label="Course Id" name="course_id"/>
    <s:submit/>
  </s:div>
</s:form>
<script type="text/javascript">
  $(document).ready(function(){
    $('#RegistrationType').change(function(){
      $('#'+this.value).css('display', 'block');
      if (this.value=='Student') $('#Faculty').css('display', 'none');
      else $('#Student').css('display', 'none');
    });
  });
</script>

OTHER TIPS

You can use Javascript's onSelect event on s:select tag to load the next fields in form depending upon the select of the "Registration Type".

<s:select onselect="loadForm();" name="type" label="Registration Type" list="{'Student','Faculty'}" emptyOption="false" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top