2012-02-08 62 views
0

是否有人知道爲什麼點擊搜索字段外部並隱藏自動完成結果焦點會再次激活一次?請登錄http://layot.prestatrend.com/ 請在搜索字段'ipo'處輸入例如3個字母。謝謝你的回覆!jQuery自動完成焦點再次隱藏後

+1

你能在你的問題中包括相關的代碼嗎?那麼這些答案將對未來的訪問者更有用。 – 2012-02-08 21:51:40

+0

但它是一個java腳本文件。 – 2012-02-08 22:06:45

回答

1

我想這只是插件的行爲,如果建議菜單可見,重新聚焦輸入。

當你點擊外(或使用Tab鍵無焦點的方式)輸入,觸發了 「模糊」 事件:

.blur(function() { 
    hasFocus = 0; 
    if (!config.mouseDownOnSelect) { 
     hideResults(); 
    } 
}) 

執行hideResults' executes another function hideResultsNow`這使得這一檢查:

var wasVisible = select.visible(); 
... 
if (wasVisible) 
    // position cursor at end of input field 
    $.Autocompleter.Selection(input, input.value.length, input.value.length); 

wasVisible因爲建議菜單已打開,所以這是真的。

$.Autocompleter.Selection任務是設置文本選擇輸入和結束時,它着重輸入:

$.Autocompleter.Selection = function(field, start, end) { 
    if (field.createTextRange) { 
     ... 
    } else if (field.setSelectionRange) { 
     ... 
    } else { 
     ... 
    } 
    field.focus(); 
}; 

如果你輸入外再次點擊,該變量wasVisible是假的,因爲建議菜單不再打開,並且$.Autocompleter.Selection未被執行,因此輸入未被重新聚焦。

+0

那麼如何去除這種重新聚焦行爲呢? – 2012-02-08 22:15:47

+0

我不知道。你確定你使用的是最新版本的插件嗎?不知道你正在使用哪個插件,很難幫助你。 – 2012-02-08 22:23:31

+0

不,它不是最新的,因爲它是CMS Prestahop的一部分。版本是自動完成的 - jQuery插件1.0.2,版權所有(c)2007 Dylan Verheul,Dan G. Switzer,Anjesh Tuladhar,JörnZaefferer – 2012-02-08 22:30:11

0

我發現的唯一方法:銷燬焦點事件上的自動完成以重新初始化它。

function defaultFocusAction(e, options) { 
    if($(e.currentTarget).val() == '') { 
    $(e.currentTarget).autocomplete('destroy'); 
    $(e.currentTarget).autocomplete(options); 
    } 
    $(e.currentTarget).trigger('keydown.autocomplete'); 
} 
var options = { 
    autoFocus : false, 
    delay : 0, 
    minLength: 0, 
    source: ['foo', 'bar'] 
}; 
$('input.autocomplete').autocomplete(options).focus(function(e) { 
    defaultFocusAction(e, options); 
});