2

This is my jsp code that i've used for populate my form with the database value dinamically. This code works fine. "maritalStatusList" is the model that returns by spring jdbc template.

<select name="MaritalStatus">
                    <c:forEach var="maritalStatus" items="${maritalStatusList}">

                        <option value="${maritalStatus.getMaritalStatusId() }">${maritalStatus.getMaritalStatusNameEng()
                            }</option>

                    </c:forEach>


            </select>

My dtabase is look like this:

maritial_status_id   |      maritial_status

 1               |            married
 2               |           unmarried

Now, there is a search button in my form which returns the id of marital status searched by mobile number. I need to auto populate this dropdown list with the searched value. How can i do that ?

(Auto populated means selected)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Amir Hossain
  • 184
  • 11
  • do you mean, populate the search bar as you type the mobile number. Its the auto suggest functionality. – Jobin May 20 '14 at 12:17
  • Nop. my page is reloaded when the search button clicked. search result returned by dao. This form will recieved the value and show the married /unmarried **selected** – Amir Hossain May 20 '14 at 12:19
  • so you want to display the result, right ? You can display the value of the variable which stores the marital status from db. – Jobin May 20 '14 at 12:33

1 Answers1

3

if I understand correctly, you have your status_id as return of the search action.

You can add the id attribute to the select:

<select name="MaritalStatus" id="MaritalStatus">
     <c:forEach var="maritalStatus" items="${maritalStatusList}">
          <option value="${maritalStatus.getMaritalStatusId()}">${maritalStatus.getMaritalStatusNameEng() }</option>
      </c:forEach>
</select>

And then you can select the option with jQuery:

<script>
$(document).ready(function(){
    $('#MaritalStatus option[value='+  ${consumer.getMaritalStatus()}  +']').attr("selected","selected");
});
</script>
Amir Hossain
  • 184
  • 11
dauricchio
  • 56
  • 7