Ajax request back JSON object fill in drop-down box
The more useless code is as follows.
$ (function () {
$.ajax ({
URL: "Http://localhost:8080/nutz/user/xx",
DataType: "JSON",
Success:function (data) {
var d= data;
for (Var i=0;i<d.length;i++) {
var t= ' <option value= ' +d[i].id+ ' > ' +d[i].provincename+ ' </option> ';
Set value and list parameters for value
var $ts =$ (t);
$ ("#sel"). Append ($ts);
}
},
Error:function () {
Alert ("+_+");
}
});
Get parameters
$ ("#sel"). Change (function () {
var pro=$ (this). Val ();
City (PRO);
});
});
Function City (PRO) {
$ ("#city"). empty ();
$ ("#city"). Prepend ("<option value= ' 0 ' > Please select City </option>");
$ ("#districts"). empty ();
$ ("#districts"). Prepend ("<option value= ' 0 ' > Please select Region </option>");
$.ajax ({
URL: "Http://localhost:8080/nutz/user/city",
data:{"pid":p ro},
DataType: "JSON",
Success:function (data) {
var d= data;
for (Var i=0;i<d.length;i++) {
var tt= ' <option value= ' +d[i].id+ ' > ' +d[i].cityname+ ' </option> ';
Set value and list parameters for value
var $ts =$ (TT);
$ ("#city"). Append ($ts);
}
},
Error:function () {
Alert ("+_+");
}
});
Get parameters
$ ("#city"). Change (function () {
var cit=$ (this). Val ();
districts (CIT);
});
}
function districts (CIT) {
$ ("#districts"). empty ();
$ ("#districts"). Prepend ("<option value= ' 0 ' > Please select Region </option>");
$.ajax ({
URL: "Http://localhost:8080/nutz/user/districts",
data:{"cid": cit},
DataType: "JSON",
Success:function (data) {
var d= data;
for (Var i=0;i<d.length;i++) {
var tt= ' <option value= ' +d[i].id+ ' > ' +d[i].districtname+ ' </option> ';
Set value and list parameters for value
var $ts =$ (TT);
$ ("#districts"). Append ($ts);
}
},
Error:function () {
Alert ("+_+");
}
});
}
</script>
<body>
<div>
<select id= "sel" width= ">"
<option value= "0" > Please select Province </option>
</select>
<select id= "City" width= ">"
<option value= "0" > Please select City </option>
</select>
<select id= "Districts" width= ">"
<option value= "0" > Please select Region </option>
</select>
</div>
</body>
Ajax cascading drop-down list box