gpt4 book ai didi

javascript - jQuery 自动完成 ui - 真的很奇怪 1 个字母延迟吗?

转载 作者:行者123 更新时间:2023-11-28 20:03:46 26 4
gpt4 key购买 nike

我在 jQuery Autocomplete UI 方面遇到一些问题。我有一个输入,我获取该输入的值来进行 JSON 搜索,从数据库中提出建议。

问题是,如果我在输入中输入 test ,则自动完成之前的打印值为 test ,但是当我在自动完成中打印它时,该值为测试。换句话说,当我打开并使用自动完成功能进行搜索时,出现了 1 个字母的延迟。

我的代码(已编辑):

input.on('keyup propertychange paste', function(event){
console.log("Inserted: " + myTextFromInput);

input.autocomplete({
delay: 0,
minLength: 0,
disabled: false,
source: function(request, response) {

// I need this value in my code
originalValue = input.val();

// Printing the current value of this variable
console.log("Searched: " + myTextFromInput);

if (myTextFromInput != null){

// Remove all "?" from this string
term = myTextFromInput.replace('?','')

// Call the method to search in database
populate(term, response)

// Filtering
result = $.ui.autocomplete.filter(result, request.term)

// Prevent a lot of "loading" items
for (var i in result){
if (result[i].type === "loading"){
var deleteItem = result.indexOf(i)
result.splice(deleteItem, 1);
}
}

// Add item "loading" to array
var item = {};
item.type = 'loading';
item.label = "Loading..";
item.value = "";

result.push(item);
}
}
})

var populate = function(term, response) {
$.getJSON(
'text.json', // Rails, you don't need to understand
{search: term},
function(json) {
var result = [];
$.each(json, function(key, value) {
var item = {};
item.type = ' ';
item.label = value.name;
item.value = value.name;
result.push(item);
}) //END EACH

var item = {};
item.type = "noneResult";
item.label = "Sent us your suggestion ('" + term + "').";
item.value = term;
result.push(item)

response(result);
}
); //END FUNCTION
};
});

这是我从控制台得到的内容,例如:

>> Inserted: myAwesomeTest
>> Searched: myAwesomeTes

我该怎么做才能消除这封信的延迟?

最佳答案

console.log("Inserted: " + myTextFromInput);

input.autocomplete({
delay: 0,
minLength: 0,
disabled: false,
source: function(request, response) {

// I need this value in my code
originalValue = input.val();

// Printing the current value of this variable
console.log("Searched: " + myTextFromInput);

if (myTextFromInput != null){

// Remove all "?" from this string
term = myTextFromInput.replace('?','')

// Call the method to search in database
populate(term, response)

// Filtering
result = $.ui.autocomplete.filter(result, request.term)

// Prevent a lot of "loading" items
for (var i in result){
if (result[i].type === "loading"){
var deleteItem = result.indexOf(i)
result.splice(deleteItem, 1);
}
}

// Add item "loading" to array
var item = {};
item.type = 'loading';
item.label = "Loading..";
item.value = "";

result.push(item);
}
}
})

var xhr;

var populate = function(term, response) {
if(xhr && xhr.readyState != 4){
xhr.abort();
}


xhr = $.getJSON(
'<%= my_path %>.json', // Rails, you don't need to understand
{search: term},
function(json) {
var result = [];
$.each(json, function(key, value) {
var item = {};
item.type = ' ';
item.label = value.name;
item.value = value.name;
result.push(item);
}) //END EACH

var item = {};
item.type = "noneResult";
item.label = "Sent us your suggestion ('" + term + "').";
item.value = term;
result.push(item)

response(result);
}
); //END FUNCTION
};

关于javascript - jQuery 自动完成 ui - 真的很奇怪 1 个字母延迟吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21187306/

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