当用户输入一个新词时,如何使javascript自动完成重置



我目前正在使用一个js函数,该函数为html输入字段提供了自动完成功能。这很好用,但是它只适用于用户给出的第一个单词。有没有一种方法可以让自动完成函数为字符串中输入的每个单词运行?

这是的功能

$(function() {
$("#text-input").autocomplete({
source: ["Eastern Redbud", "Eastern White Pine", "Eastern Red cedar",],
minLength: 1
});
});

在此处引用可以使用document.body.onkeyup的第一个答案然后用e.keyCode==32 检查空间

当按下空格键时,调用自动完成函数,现在您必须在空格处拆分输入字段值,并使用最后一个元素将自动完成仅应用于新词。

document.body.onkeyup = function(e){
// execute when space was pressed  
if(e.keyCode == 32){

// apply the autocomplete only at the first word in the input field
let inputWords = $("#text-input").split(" ");
inputWords[inputWords.length -1].autocomplete({
source: ["Eastern Redbud", "Eastern White Pine", "Eastern Red cedar",],
minLength: 1
});
}
}

最新更新