Javascript 当文本字段不为空时,Ajax自动完成jquery不起作用
我正在使用Ajax autocomplete for jquery,我的HTML代码是Javascript 当文本字段不为空时,Ajax自动完成jquery不起作用,javascript,jquery,html,ajax,autocomplete,Javascript,Jquery,Html,Ajax,Autocomplete,我正在使用Ajax autocomplete for jquery,我的HTML代码是 <input type="text" name="autocomplete" id="globalBCCAutoComplete"> 它工作正常,但当我在文本字段中添加value属性时: <input type="text" name="autocomplete" id="globalBCCAutoComplete" value="This is test"> 增值后自动完成不起作
<input type="text" name="autocomplete" id="globalBCCAutoComplete">
它工作正常,但当我在文本字段中添加value属性时:
<input type="text" name="autocomplete" id="globalBCCAutoComplete" value="This is test">
增值后自动完成不起作用。问题出在哪里?从评论中我了解到,您所需要的只是将输入上设置的初始值附加到建议对象上,以便将其用作前缀 你为什么不这么说 使用您的代码: HTML: JSFIDLE示例: 另一种选择是ES6箭头函数和.map函数:
//append the values to the json object.
countries.map( (country) => {
country.value = initialTextValue + ' ' + country.value
});
您需要使用javascriptany在控制台中设置值?值已经设置并且来自数据库。@Se0ng11在控制台中没有错误。.我也在jquery UI中尝试过,它在我的条件下不起作用。它只显示国家变量的标记,而不是整个文本框值和John值可以更改为其他值,因为John值来自数据库,自动建议将显示国家变量,而不是整个值。
<input type="text" name="autocomplete" id="globalBCCAutoComplete" value="john">
$(document).ready(function() {
let countries = [
{ value: '{contract_name}', data: 'Contact Name'},
{ value: '{user_company}', data: 'User Company' }
];
//get current value from the textbox
let initialTextValue = $('#globalBCCAutoComplete').val().trim();
//append the value to your JSON objects.
$(countries).each(function(key, country) {
country.value = initialTextValue + ' ' + country.value
});
//the rest of your code:
$('#globalBCCAutoComplete').autocomplete({
lookup: countries,
onSelect: function (suggestion) {
console.log('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
});
//append the values to the json object.
countries.map( (country) => {
country.value = initialTextValue + ' ' + country.value
});