IE9中无法识别自动完成功能

时间:2012-07-27 04:50:16

标签: 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",
                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'
     )

非常感谢帮助!

1 个答案:

答案 0 :(得分:2)

你的问题就在这一行:

starts_with: request.term,

这是对象结构中的最后一个元素,最后有一个逗号。

这在技术上是非法的Javascript,但IE是唯一强制执行它的浏览器。这就是为什么你在IE中会出现错误而不是其他浏览器的错误。

同样的错误也出现在第33行(即几乎在代码的末尾),在对象结构的末尾有一个右括号后跟一个非法的逗号},

如果您使用JSHint等工具验证代码,则会轻易显示此错误。

希望有所帮助。

相关问题