私の問題は、次のコードが Firefox と chrome では正常に動作するが、IE9 では動作しないことです。場所が未定義または null であると表示されます。
私のオートコンプリートコードは次のとおりです。
$( "#id_location" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "{% url 'food.views.search_location' %}",
dataType: "json",
data:{
maxRows: 5,
starts_with: request.term,
},
success: function( data ) {
response( $.map( data.location, function( item ) {
return {
label: item.label,
value: item.value
}
}));
}
});
},
minLength: 1,
focus: function(event,ui){
//prevent value insert on focus
$("#id_location").val(ui.item.label);
return false; //Prevent widget from inserting value
},
select: function(event, ui) {
$('#id_location').val(ui.item.label);
$('#id_locationID').val(ui.item.value);
return false; // Prevent the widget from inserting the value.
},
});
私のバックアップコードは次のとおりです。
def search_location(request):
"""
Jason data for location
search autocomplete
"""
q = request.GET['starts_with']
r = request.GET['maxRows']
ret = []
listlocation = USCities.objects.filter(name__istartswith=q)[:r]
for i in listlocation:
ret.append({'label':i.name+','+i.state.name+' '+i.state.abbr,'value':i.id})
ret = {'location':ret}
data = simplejson.dumps(ret)
return HttpResponse(data,
content_type='application/json; charset=utf8'
)
助けていただければ幸いです。