2015-05-22 29 views
1

我使用autosuggestion插件。如何防止onSelect在已選項目上觸發

首先我代碼:

$('.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 or #jmere的其他值,但如果我再次點擊輸入字段與類.auto(之前選擇),然後onSelect再次啓動,我選擇手動#cena or #jmere值更改爲建議的值。

請幫我解決這個問題。所以,我怎麼能阻止呼叫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選項 - 防止調用時,有已經選擇了一些項目?

+0

僅供參考,'autocomplete'不是jQuery核心的一部分。我也添加了相關標籤(如果它真的是jquery-UI自動完成,您正在使用...嗎?) –

+0

嘗試在項目預先解壓或取消綁定onSelect事件時刪除.auto類 – madalinivascu

+0

否我使用devbridge autosuggestion插件,而不是jquery UI – MonkeyBusiness

回答

1

您可以通過使用disable禁用自動完成:

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

,你可以改變它回來就好:

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

jsfiddle DEMO

如果你想徹底刪除自動完成,你可以使用despose廣告OP說:

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

嗯,不工作,也許是問題因爲我使用autoDisabled成爲ajax成功:http://jsfiddle.net/to91udvb/ – MonkeyBusiness

+0

看,禁用不要在這裏excist:https://www.devbridge.com/sourcery/components/jquery -autocomplete/ – MonkeyBusiness

+0

make'var autoDisabled = false;'global,如果你在ajax中引入它,它的作用域就是這樣,所以其他ajax中的'autoDisabled只是另一個本地。 – Samurai

相關問題