gpt4 book ai didi

javascript - JQuery 自动完成源未更新

转载 作者:行者123 更新时间:2023-12-03 00:22:47 24 4
gpt4 key购买 nike

我正在使用jquery-ui autocomplete插件。

这是我实例化自动完成插件的方法。

//autofill
$( "#TextArea" ).autocomplete({
source: "search.php?option="+ option.toLowerCase(),
minLength: 3
});

在下拉菜单更改时,我正在尝试更改选项:

$('#Options').on('change',  function() {
option = this.value.toLowerCase();
var teaxtarea = $('#TextArea');
//this is supposed to change the source string when the option value changes.
teaxtarea.autocomplete( "option", "source", "search.php?option="+ option);
}
});

我从下面的问题中获得了更新源字符串的代码。

Jquery: Possible to dynamically change source of Autocomplete widget?

但是,这个解决方案似乎不适合我。

即使我更改下拉列表中的选项,我仍然会得到第一个选定的选项。非常感谢任何帮助。

最佳答案

提出以下建议:

$("#TextArea").autocomplete({
source: function(req, resp){
var option = $('#Options option:selected').val().toLowerCase();
$.ajax({
cache: false,
url: "search.php",
data: {
option: option,
term: req.term
},
dataType: "json",
success: function(data){
resp(data);
}
});
},
minLength: 3
});

我认为一个问题是如果 #Options<select>元素,您需要找到所选的子元素:

$("#Options option:selected")

这可确保您拥有正确的对象,然后您可以调用 .val()之上。如果您需要更多帮助,请使用 MCVE 更新您的帖子:https://stackoverflow.com/help/mcve

这可能会为您解决问题。如果没有,请继续。

其次,为了确保您没有缓存,我们可以执行更多手动源捕获。

When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must support CORS). The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results.

因此,我们通过一些添加的设置来复制相同的功能。因此,每次检查来源时,都会检查选项并发送术语。我们将以 JSON 形式获取数据并将其发送回自动完成以进行显示。

希望有帮助。

关于javascript - JQuery 自动完成源未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54230160/

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