当用户键入新词时,如何使javascript自动完成重置?
我目前正在使用一个js函数,该函数为html输入字段提供自动完成功能。这很好,但是它只适用于用户给出的第一个单词。有没有办法让字符串中输入的每个单词都运行autocomplete函数 下面是函数当用户键入新词时,如何使javascript自动完成重置?,javascript,jquery,forms,autocomplete,jquery-ui-autocomplete,Javascript,Jquery,Forms,Autocomplete,Jquery Ui Autocomplete,我目前正在使用一个js函数,该函数为html输入字段提供自动完成功能。这很好,但是它只适用于用户给出的第一个单词。有没有办法让字符串中输入的每个单词都运行autocomplete函数 下面是函数 $(function() { $("#text-input").autocomplete({ source: ["Eastern Redbud", "Eastern White Pine", "Eastern Red ce
$(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
});
}
}
参考第一个答案,您可以使用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
});
}
}
在输入更改事件
$(“输入”).change(函数(){alert(“文本已更改”);})上调用它
@Alex你能把你的答案扩展成一个例子吗?很抱歉,上面的回答不起作用,但我想我有另一个解决方案,我会在输入更改事件$(“输入”).change(函数(){alert(“文本已更改”);})
@Alex你能把你的答案扩展成一个例子吗?很抱歉,上面的回答不起作用,但我想我有另一个解决方案,我会写一个答案