gpt4 book ai didi

javascript - 如何防止 onSelect 在已选择的项目上触发

转载 作者:行者123 更新时间:2023-11-28 00:16:50 24 4
gpt4 key购买 nike

我使用自动建议插件。

首先我有代码:

$('.auto').autocomplete({

lookup: data,

onSelect: function (suggestion) {

console.log(koji);
$('#cena_'+koji).val(suggestion.cena);
console.log(suggestion.ID);
$('#jmere'+koji).val(suggestion.jmere);
$('#kol'+koji).val(suggestion.kol);
$('#popust'+koji).val(suggestion.popust);
$('#pdv'+koji).val(suggestion.porez);
etc...

这没关系,所以当我从列表中选择一些项目时,我会更新其他输入字段。

但之后我只是为 #cena 或 #jmere 选择其他值,但如果我再次单击类 .auto 的输入字段(之前选择的),则再次触发 onSelect 并获取值我选择手动将 #cena 或 #jmere 更改为建议值。

请帮我解决这个问题。那么,当已经选择了 Item 时,如何防止调用 onSelect...

onSelect的JS代码为:

onSelect: function (index) {
var that = this,
onSelectCallback = that.options.onSelect,
suggestion = that.suggestions[index];

that.currentValue = that.getValue(suggestion.value);

if (that.currentValue !== that.el.val() && !that.options.preserveInput) {
that.el.val(that.currentValue);
}

that.signalHint(null);
that.suggestions = [];
that.selection = suggestion;

if ($.isFunction(onSelectCallback)) {
onSelectCallback.call(that.element, suggestion);
}
},

那么有什么方法可以更改 onSelect 选项 - 以防止在已经选择了某些项目时调用?

最佳答案

您可以使用disable禁用自动完成功能:

$('SELECTOR').autocomplete('disable');

你可以把它改回来,比如:

$('SELECTOR').autocomplete('enable');

jsfiddle DEMO

如果你想完全删除自动完成功能,你可以使用despose广告OP说:

$('SELECTOR').autocomplete('dispose');

关于javascript - 如何防止 onSelect 在已选择的项目上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393574/

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