gpt4 book ai didi

javascript - 如何完全删除 JQuery 自动完成中使用的默认查询字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:13 25 4
gpt4 key购买 nike

在以下情况下,我无法弄清楚如何使用 JQuery 自动完成功能。

我正在尝试将它连接到返回 JSON 对象的 Web 服务。 URL 形式如下:

host/api/schools/{search-term}

没有查询字符串 ?term= 或任何其他内容。

如何设置自动完成来执行此操作?我是 JavaScript 的新手,但我可以应付。

我已经尝试了以下位:

$(...).autocomplete({
source: function(term, callback) {
$.getJSON("url", { foo: term }, callback);
}
});

但不能完全将 host/api/schools/{search-term} 传递给服务器(输出为 host/api/schools/?foo% 5Bterm=%5D{search-term} 当使用 fiddler 检查时 {search-term} 是输入的文本。

帮忙吗?

最佳答案

在此处添加它,因为它基本上回答了来自 jquery-ui 文档的问题:

The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo"

A response callback, which expects a single argument to contain the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data (String-Array or Object-Array with label/value/both properties). It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.

关于javascript - 如何完全删除 JQuery 自动完成中使用的默认查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8510866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com