有没有办法可以将此autocomplete
用于多个文本框元素。我面临的困难是我想根据调用函数的文本框动态更改元素绑定和源。
$("#element").autocomplete({
source: "../ajax/autocompletes/trucks.php",
minLength: 0,
select: function (event, ui) {
$("#element_id").val(ui.item.id);
},
change: function (event, ui) {
if (!ui.item) {
this.value = "";
alert('PLEASE SELECT AN ITEM FROM DROPDOWN!');
}
}
}).dblclick(function () {
$(this).autocomplete("search");
});
我搜索了谷歌但没有找到太多。
答案 0 :(得分:1)
试试这个并告诉我,如果你需要进一步的帮助
function multiAutocomplete(element,sourceUrl){
$(element).autocomplete({
source: sourceUrl,
minLength: 0,
select: function (event, ui) {
$("#element_id").val(ui.item.id);
},
change: function (event, ui) {
if (!ui.item) {
this.value = "";
alert('PLEASE SELECT AN ITEM FROM DROPDOWN!');
}
}
}).dblclick(function () {
$(this).autocomplete("search");
});
}
这样打电话:
multiAutocomplete('#element','../ajax/autocompletes/trucks.php');
multiAutocomplete('#element2','../ajax/autocompletes/trucks2.php');
multiAutocomplete('#element3','../ajax/autocompletes/trucks3.php');