jquery自动完成未定义的函数

时间:2014-08-03 19:48:17

标签: jquery django

我试图使用jquery自动完成功能但是得到一个未定义的函数错误 -

html代码& js代码:

$(document).ready(function(){
     $("#searchtext").autocomplete({

        //source: availableTags, This works.....(static list are working)
        source: function (request, response) {   // This is not working 
            $.ajax ({
                url: '/autosuggest/',
                data: { 'keyword':$('#searchtext').val()},
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset = utf-8",
                success: function (data) {
                    return data
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }

            });
        },
        minLength: 1,
    //open: function() { $(this).autocomplete("widget").css({"width": $(this).width()+6})},
    //appendTo:$( ".cls_comp").parent(),
        select: function( event, ui ) { }
    });
})

views.py

@csrf_exempt
def autosuggest(request):
    json_response = ['value1','value2','value3','value4']
    return HttpResponse(json_response,content_type='application/json')

错误:

 $("#searchtext").autocomplete({

Uncaught type:undefined is not a function

使用jquery lib:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

1 个答案:

答案 0 :(得分:4)

你包括jQuery两次。第二个是覆盖第一个,其中包含jQuery UI方法。将您的script代码更改为:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>