Jquery IE9中未识别自动完成
我的问题是以下代码在firefox和chrome中运行良好,但在IE9中不起作用。它表示位置未定义或为空 我的自动完成代码如下:Jquery IE9中未识别自动完成,jquery,python,jquery-ui,internet-explorer-9,jquery-ui-autocomplete,Jquery,Python,Jquery Ui,Internet Explorer 9,Jquery Ui Autocomplete,我的问题是以下代码在firefox和chrome中运行良好,但在IE9中不起作用。它表示位置未定义或为空 我的自动完成代码如下: $( "#id_location" ).autocomplete({ source: function( request, response ) { $.ajax({ url: "{% url 'food.views.search_location' %}", dataType: "json",
$( "#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'
)
我的备份代码如下:
$( "#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'
)
非常感谢您的帮助 您的问题在这一行:
starts_with: request.term,
这是对象结构中的最后一个元素,它的末尾有一个逗号
从技术上讲,这在Javascript中是非法的,但IE是唯一强制执行它的浏览器。这就是为什么你会在IE中出错,而不是在其他浏览器中
同样的错误也发生在第33行(即几乎在代码的末尾),其中在对象结构的末尾有一个右大括号,后跟一个非法的逗号},
如果在工具(如)中验证代码,则很容易出现此错误
希望对您有所帮助。您在哪里得到未定义或空的位置?在服务器端还是在JS中?您还可以告诉代码中错误发生的确切位置吗?