美国的state,city,zip三级菜单
js:
$("#id_state").change(function(event){
var state=$(this).val();
$.ajax({
type: "GET",
url: "/profile/get_city/"+state,
dataType:"json",
success: function(json){
$("#id_city option").each(function(){
$(this).remove();
});
for(var i=0;i<json.length;i++){
$("<option value='"+json[i]+"'>"+json[i]+"</option>").appendTo($("#id_city"));
}
}
});
});
$("#id_city").change(function(event){
var city=$(this).val();
$.ajax({
type: "GET",
url: "/profile/get_zip/"+city,
dataType:"json",
success: function(json){
$("#id_postcode option").each(function(){
$(this).remove();
});
for(var i=0;i<json.length;i++){
$("<option value='"+json[i]+"'>"+json[i]+"</option>").appendTo($("#id_postcode"));
}
}
});
});
view:
def get_city(request,state):
city=Zipcode.objects.filter(state=state).values('city').distinct()
city_list=[]
for i in city:
city_list.append(i['city'])
return HttpResponse(simplejson.dumps(city_list))
def get_zip(request,city):
zip=Zipcode.objects.filter(city=city).values('zip').distinct()
zip_list=[]
for i in zip:
zip_list.append(i['zip'])
return HttpResponse(simplejson.dumps(zip_list))