You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jinja+Bootstrap中jQuery autocomplete is not a function错误排查

问题

我正尝试在继承自base.html的Jinja模板中,为表单内id为suburbs的文本输入框实现jQuery autocomplete功能,但控制台出现「Autocomplete is not a function」错误。已尝试使用jquery.noConflict(),也确认了jQuery与jQuery UI的加载顺序无误,仍无法解决问题。

相关代码

Jinja模板代码

{% extends "base.html" %}
{% block title %}Renter Information{% endblock %}

{% block head %}
    {{ super() }}
    <link rel = "stylesheet" href = "{{ url_for('static', filename = 'css/form.css') }}">
{% endblock %}

{% block content %}
    {% block page_content %}
    {{ super() }}
    <h1>Rental Property Information Form</h1>
    <p>Fill out the below form and a CSV containing Listing Date, Property Type, Suburb, State, Postcode,
    Property Address, Weekly Rent, Annual Rent, Land Area (if applicable) and URL will be sent to your email address.</p>
    <br>
    {{ wtf.quick_form(form, novalidate=True) }}
    {% endblock %}
{% endblock %}

base.html代码

{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}{% endblock %}
{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/base.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='housing-icon-9.jpg') }}" type="image/x-icon">
<link rel="icon" href="{{ url_for('static', filename='housing-icon-9.jpg') }}" type="image/x-icon">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
{% endblock %}

{% block content %}
<div class="container">
    {% block page_content %}
    {% for message in get_flashed_messages() %}
    <div class="alert no results">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        {{ message }}
    </div>
    {% endfor %}
    {% endblock %}
</div>
{% endblock %}

script.js代码

$(document).ready(function () {

    $('#suburbs').autocomplete({
        source: ["Eastern Red", "Eastern Blue"],
        minlength: 2
    });
})

排查与修复方案

1. 解决jQuery重复引入冲突

bootstrap/base.html本身会自动引入jQuery,你在base.html的head块里手动引入的第二次jQuery会覆盖原有版本,导致jQuery UI绑定的方法丢失。

修复:
删除base.html中手动引入的jQuery脚本,保留Bootstrap自带的jQuery即可:

{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/base.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='housing-icon-9.jpg') }}" type="image/x-icon">
<link rel="icon" href="{{ url_for('static', filename='housing-icon-9.jpg') }}" type="image/x-icon">
<!-- 移除手动引入的<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
{% endblock %}

2. 确认表单元素id正确性

使用wtf.quick_form生成表单时,输入框的id默认对应表单类中字段的名称。打开浏览器开发者工具,检查目标输入框的实际id是否为suburbs:

  • 如果字段名是suburb(单数),则id为suburb,需将script.js中的选择器改为$('#suburb')
  • 若字段名确实是suburbs,则无需修改选择器

3. 检查jQuery UI加载状态

在浏览器「网络」面板中,确认jquery-ui.min.js的加载状态为200(成功加载)。若加载失败,可替换为稳定CDN链接:

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

4. 修正script.js拼写错误

minlength需改为驼峰式的minLength,否则无法触发最小输入长度限制(虽不是报错直接原因,但影响功能正常运行):

$(document).ready(function () {
    $('#suburbs').autocomplete({
        source: ["Eastern Red", "Eastern Blue"],
        minLength: 2
    });
})

内容的提问来源于stack exchange,提问作者user21307743

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:52:24