我的问题是以下代码在 Firefox 和 chrome 中运行良好,但在 IE9 中不起作用。它说位置未定义或为空。
我的自动完成代码如下:
$( "#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'
)
帮助将不胜感激!