【问题标题】:jQuery Create an Autocomplete FormjQuery 创建一个自动完成表单
【发布时间】:2018-12-22 19:45:38
【问题描述】:

我需要您在 adjust.html 中的搜索表单方面提供帮助。它应该从用户“q”获取输入,并从数据库表“book”[.用户应选择其中一项建议并按下“添加”按钮。

当我运行程序时,我收到以下错误:“RuntimeError: Missing input”. 感谢您的想法和更正。

这是我的 JavaScript、Python 和 HTML 代码:

function configure()
    {
    // Configure typeahead
    $("#q").typeahead({
        highlight: false,
        minLength: 1
    },
    {
        display: function(suggestion) { return null; },
        limit: 10,
        source: search,
        templates: {
            suggestion: Handlebars.compile(
                "<div>"+
                "{{Title}}, {{Author}}" +
                "</div>"
            )
       },
        updater: function(item) {
        //take selected item and submit to the form
        this.$element[0].value = item;
        this.$element[0].form.submit();
        return item;
        }
    });
    // Hide info window when text box has focus
    $("#q").focus(function(eventData) {
        info.close();
    });

    // Give focus to text box
    $("#q").focus();
}

// Search database for typeahead's suggestions
function search(query, syncResults, asyncResults)
{
    // Get places matching query (asynchronously)
    let parameters = {
        q: query
    };
    $.getJSON("/adjust", parameters, function(data, textStatus, jqXHR) {

        // Call typeahead's callback with search results (Author Title)
        asyncResults(data);
    });
}

python 代码:

@app.route("/adjust", methods=["GET", "POST"])
@login_required
def search():
    """Search for books that match query"""
    if request.method == "GET":
        if not request.args.get("q"):
            return render_template("adjust.html")
        else:
            if request.args.get("q"): # handle ajax request
                q = request.args.get("q")  + "%"
            else:
                q = request.args.get("q")  + "%" # handle form submit

            books = db.execute("SELECT Title, Author FROM book WHERE Title LIKE :q OR Author LIKE :q", q=q)
    return jsonify(books)

html代码:

{% extends "layout.html" %}
{% block title %}
    Add your Book
{% endblock %}
{% block main %}
    <form action="/adjust">
        <div class="form-group">
        <p>Choose your Book</p>
        <label class="sr-only" for="q">Title, Author</label>
        <input class="form-control" id="q" placeholder="Title, Author" type="text"/>
        </div>
        <button class="btn" type="submit">Add</button>
    </form>
{% endblock %}

【问题讨论】:

  • 你能发帖或截图错误吗
  • @ewwink 我在问题中添加了屏幕截图。
  • 我尝试了上面的所有代码,它按预期工作。你能用完整的 html 和 javascript 创建 jsfiddle 吗?
  • 谢谢@ewwink,这是完整的代码。我认为问题出在py和js之间的连接处。 [链接] (github.com/nailya6445/cs50_final_project)

标签: jquery sqlite autocomplete forms cs50


【解决方案1】:

错误不是来自Javascript,而是flask无法为sql呈现adjust.html,令牌不正确,不是...LIKE: q而是...LIKE :q

这里是固定代码

@app.route("/adjust", methods=["GET", "POST"])
@login_required
def search():
    """Search for books that match query"""
    if request.method == "GET":
        if not request.args.get("q"):
            return render_template("adjust.html")
        else:
            if request.args.get("q"): # handle ajax request
                q = request.args.get("q")  + "%"
            else:
                q = request.form.get("q") + "%" # handle form submit

            books = db.execute("SELECT Title, Author FROM book WHERE Title LIKE :q OR Author LIKE :q", q=q)
    return jsonify(books)

【讨论】:

  • 非常感谢您的帮助。它几乎可以正常工作。你能解释一下为什么你对“ajax request”和“form submit”使用不同的函数吗?我想用选定的搜索结果填写我的表格。例如,如果用户从建议中选择项目,它应该出现在搜索表单中 - 就像在谷歌搜索中一样。 (请参阅 js 文件中的更改)。我现在应该更改“#handle form submit”部分吗?
  • 它是按照原代码分开的,你可以根据需要删除表单提交。请为另一个问题创建另一个帖子解决这个问题需要时间。
  • ok)这里是:[link] (stackoverflow.com/questions/53923637/…)
猜你喜欢
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
相关资源
最近更新 更多