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());
}
%>
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
countfield (as the original poster in the other thread does), as you pass back one of the following values:and you are checking for:
What you are looking for never exists, so you will never return any data for the second combo.