jquery 自动完成未定义函数

标签 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 ) { }
    });
})

View .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 库:

<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>

最佳答案

您两次包含 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>

关于jquery 自动完成未定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108467/

相关文章:

jquery - 使用 AJAX 的数据表分页

jquery - AJAX 加载的内容会影响 SEO/搜索引擎吗?

mysql - django 过滤器正则表达式在 python 重新工作时不起作用

python - MySQL 检查一周中每一天的营业时间是否存在

python - Django 选择一个有效的选项。[...] 不是可用的选项之一。以动态生成的形式

javascript - 如何使用 .addClass 设置下拉列表项

php - 如何创建自定义 jquery 轮播?

python - 打印可能存在也可能不存在的嵌套字典值

php 数据未处理,有人可以查看我的代码吗?

python - django.core.exceptions.AppRegistryNotReady : Apps aren't loaded yet. django 1.8