gpt4 book ai didi

jquery 自动完成不过滤数据

转载 作者:行者123 更新时间:2023-12-03 22:44:28 25 4
gpt4 key购买 nike

我正在研究 Jquery UI 自动完成功能,以根据我输入的文本提取数据和 piopulate。

  1. 在文本框中输入文本时会填充数据
  2. 问题是数据没有根据我输入的内容进行过滤。

下面的代码会出现什么问题

<input type=text id="tbxPG">

<script type="text/javascript">
$(document).ready(function (){
$("#tbxPG").autocomplete({
source: function (request, response) {
$.ajax({
dataType: "json",
data: { term: request.term, },
type: 'Get',
contentType: 'application/json; charset=utf-8',
xhrFields: { withCredentials: true },
crossDomain: true,
cache: true,
url: 'myURL',
success: function (data) {
response($.map(data.value, function (item) { return { label: item.Name, value: item.Name } })); }, error: function (data) {

}
});
},
minLength: 3,
open: function () {

},
close: function () {

},
focus: function (event, ui) {

},
select: function (event, ui) {

}
});
});
</script>

最佳答案

由于您有一个ajax请求来获取数据,因此最好从服务器发送过滤后的数据,而不是每次都加载数据并在本地过滤。

但如果你做不到,在最坏的情况下尝试

$(document).ready(function () {
$("#tbxPG").autocomplete({
source: function (request, response) {
$.ajax({
dataType: "json",
data: {
term: request.term,
},
type: 'Get',
contentType: 'application/json; charset=utf-8',
xhrFields: {
withCredentials: true
},
crossDomain: true,
cache: true,
url: 'myURL',
success: function (data) {
var array = $.map(data.value, function (item) {
return {
label: item.Name,
value: item.Name
}
});

//call the filter here
response($.ui.autocomplete.filter(array, request.term));
},
error: function (data) {

}
});
},
minLength: 3,
open: function () {

},
close: function () {

},
focus: function (event, ui) {

},
select: function (event, ui) {

}
});
});
<小时/>

另一个解决方案是在 dom 就绪时加载资源,然后使用该数组创建 aucomplete

$(document).ready(function () {
//load the array first, it will happen only once - this is to be done if you are dealing with a small static data set
$.ajax({
dataType: "json",
data: {
term: request.term,
},
type: 'Get',
contentType: 'application/json; charset=utf-8',
xhrFields: {
withCredentials: true
},
crossDomain: true,
cache: true,
url: 'myURL',
success: function (data) {
var array = $.map(data.value, function (item) {
return {
label: item.Name,
value: item.Name
}
});

//create the auto complete once the ajax request is completed
$("#tbxPG").autocomplete({
source: array,
minLength: 3,
open: function () {

},
close: function () {

},
focus: function (event, ui) {

},
select: function (event, ui) {

}
});
},
error: function (data) {

}
});
});
<小时/>

如果你想缓存,另一个解决方案是使用本地缓存(使用变量),如下所示(未测试) - 这里数组仅加载一次,如果它已经加载,那么我们不再使用ajax,而是使用数组的值

$(document).ready(function () {
var array;
$("#tbxPG").autocomplete({
source: function (request, response) {
if (array) {
response($.ui.autocomplete.filter(array, request.term));
} else {
$.ajax({
dataType: "json",
data: {
term: request.term,
},
type: 'Get',
contentType: 'application/json; charset=utf-8',
xhrFields: {
withCredentials: true
},
crossDomain: true,
cache: true,
url: 'myURL',
success: function (data) {
array = $.map(data.value, function (item) {
return {
label: item.Name,
value: item.Name
}
});

//call the filter here
response($.ui.autocomplete.filter(array, request.term));
},
error: function (data) {

}
});
}
},
minLength: 3,
open: function () {

},
close: function () {

},
focus: function (event, ui) {

},
select: function (event, ui) {

}
});
});

关于jquery 自动完成不过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871172/

25 4 0