2012-01-09 152 views
2

我正在使用jQuery自動完成。這裏是我的
HTML自動完成minChars屬性

<input class="autocomplete_input"> 

JS

$(".autocomplete_input").autocomplete({ 
    source: autocompleteOptions 
}); 

// autocompleteOptions is a array which contains all values for autocomplete 

一切工作正常。我想要顯示關於onFocus的所有建議。在Google上搜索了一些類似的問題之後,我發現了Autocomplete的minChars屬性。我試過但仍然沒有運氣

$(".autocomplete_input").autocomplete({ 
    source: autocompleteOptions, 
    minChars:0 
});  

我該如何正確使用minChars

EDIT1:
我用這link。謝謝羅裏。
在給定的鏈接我找不到minLength財產。
EDIT2:
我試圖

$(".autocomplete_input").autocomplete({ minLength: 0, source: autocompleteOptions}); 

還是建議不顯示在onFocus。我注意到的一個區別是,如果我鍵入任何字符,則會顯示相應的結果,如果使用反斜槓刪除該字符,則會顯示所有建議。

回答

4

您使用的是jQuery UI Autocomplete

正確的參數會minLength

$(".selector").autocomplete({ minLength: 0, source: autocompleteOptions }); 
+0

是我使用的是相同的。如何同時定義'source'和'minChar/minLength'? – xyz 2012-01-09 11:44:32

+0

'.autocomplete {{minLength:0,source:「source here」});'如我更新的回答所示 – 2012-01-09 11:48:01

+0

我正在使用http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions。在這裏,我可以看到'minChars' – xyz 2012-01-09 11:56:14

0

嘗試使用focus事件並調用自動完成手動顯示:

$(".autocomplete_input").autocomplete({ 
    source: autocompleteOptions 
}).focus(
    function() { 
     if (this.value == "") { 
       $(this).autocomplete('search', ''); 
      } 
     } 
    ); 
+0

謝謝。我認爲更容易的選擇將在那裏。或者我會去爲你的。 – xyz 2012-01-09 11:58:38

0

這裏是jQuery的自動完成的文檔頁面: http://jqueryui.com/demos/autocomplete/

您正在尋找的選項是minLength

$(".autocomplete_input").autocomplete({ 
source: autocompleteOptions, 
minLength:0 
});  
+0

請檢查我的編輯。 – xyz 2012-01-09 11:59:01

1

假設this是你使用的插件,試試這個:

$(".autocomplete_input").autocomplete(
    autocompleteOptions, 
    { minChars: 0 } 
}); 
+0

感謝您的鏈接。是的,我正在使用相同的。我試過你的建議,但得到錯誤'錯誤:this.source不是函數 源文件:http:// localhost:7009/portal/javascript/jquery-ui-1.8.10.custom.min.js 行:322 ' – xyz 2012-01-09 11:55:22

+0

請檢查我的編輯。 – xyz 2012-01-09 12:05:26

1
 
/* 
* jQuery Autocomplete plugin 1.2.3 
* 
* Copyright (c) 2009 Jörn Zaefferer 
* 
* Dual licensed under the MIT and GPL licenses: 
* http://www.opensource.org/licenses/mit-license.php 
* http://www.gnu.org/licenses/gpl.html 
* 
* With small modifications by Alfonso Gómez-Arzola. 
* See changelog for details. 
* 
*/ 

in line 588 change this 

[[[ for (var i = q.length - 1; i >= options.minChars; i--) { ]]] 

for this 

[[[ for (var i = q.length - 1; i >= 0; i--) { ]]]