0

I tried, googled a lot but still finding for a solution. My problem is simple just i want to populate child combo box in index.jsp after an onchange event in parent combo box. I have used JSON array objects to retrieve but not populating child combo, any help please? When i select ABC then automatically 001 will be populated in second combo box, similarly when i select PQR then 002 will be populated and 001 will be removed.

index.jsp

 <script type="text/javascript">
 $(document).ready(function() {
 $("#combobox1").change(function() {// by onchange event in parent combo box i want to populate 
 child combobox 
$.getJSON('state.jsp', {count : this.value}, function(responseData) {
$("#combobox2").append(
$("<option></option>").html(responseData.name).val(responseData.name)
);
});
});
});          
</script>

 <body>  
//parent combo box
<select id="combobox1" name="count">
<option value="abc">ABC</option>
<option value="pqr">PQR</option>
</select>
 // child combo box

<select id="combobox2" name="combobox2">// here i want to populate data `001` by `ABC` or `002` by selecting `PQR` , how can i do it?
<option value="">select</option>
</body> 

state.jsp

 <%@page import="net.sf.json.JSONObject"%>
 <%@page import="net.sf.json.JSONArray"%>
 <%
 String count=request.getParameter("count");
 if(count.equals("ABC"){
 JSONObject arrayObj= new JSONObject();
   arrayObj.put("name","001");// how to populate `001` in an option in child combo box when `ABC` will be selected in parent combo?
  response.setContentType("application/json");
  response.getWriter().write(arrayObj.toString());
}
else if (count.equals("PQR"){
 JSONObject arrayObj= new JSONObject();
arrayObj.put("name","002");// how to populate `002` in an option in child combo box when `PQR` will be selected in parent combo?
  response.setContentType("application/json");
  response.getWriter().write(arrayObj.toString());
}
%>
Tom
  • 761
  • 7
  • 22
  • 41
  • You aren't using jquery, so why tag it that way? On a side note, using jQuery would make your life a lot easier. – twilson Feb 11 '12 at 14:06
  • i have tried with jquery by seeing examples here, but it does not trigger immediately after onchange in parent combo, i have to select from that drop down menus – Tom Feb 11 '12 at 14:10
  • Can you post your jQuery examples? – twilson Feb 11 '12 at 14:11
  • i have edited my question, please see and suggest any idea – Tom Feb 11 '12 at 14:30
  • Is this some homework, cos this is the second, almost identical, use of this I've seen today. – twilson Feb 11 '12 at 14:33
  • i have taken all codes from this forum http://stackoverflow.com/questions/9238267/clean-old-options-from-child-combo-box-when-receiving-data-by-json-array-objects are you talking about this? – Tom Feb 11 '12 at 14:34
  • i slightly modified and posted but your answer in that forum did not work for me hence i asked – Tom Feb 11 '12 at 14:40
  • The error is likely in your JSP then, as the jQuery and Javascript is sound. – twilson Feb 11 '12 at 14:46

1 Answers1

1

Using my example code provided in Clean old options from child dropdown when receiving data by JSON

You need to case insensitive comparison of the count field (as the original poster in the other thread does), as you pass back one of the following values:

  • abc
  • pqr

and you are checking for:

  • ABC
  • PQR

What you are looking for never exists, so you will never return any data for the second combo.

Community
  • 1
  • 1
twilson
  • 2,062
  • 14
  • 19