[英]Lazy initialization of jquery UI autocomplete
我想懶惰地初始化jQueryUI自動完成的源選項。 我知道可以將函數傳遞給source選項的可能性,但是我想延遲初始化,直到用戶真正開始使用自動完成功能為止。
我嘗試等待搜索事件,然后設置source選項,但這無效。
var availableTags = [
"ActionScript",
"AppleScript",
"Scheme"
];
$("#searchFirstTransactionStateInfo_searchPartnerId").autocomplete({
source : [],
minLength: 0,
search: function( event, ui ) {
$(event.target).source = availableTags;
}
});
如何延遲設置源選項,直到用戶開始使用UI元素?
注意:為簡單起見,我使用了固定數組。 最后,我想調用一個AJAX函數。
好吧,如果您使用一個函數作為源參數,並讓該函數執行一個get請求(過程的ajax),則實際上它只會在您鍵入內容后才開始發出請求。 例如,打開您的網絡選項卡,然后在此UI示例中開始輸入:
http://jqueryui.com/autocomplete/#multiple-remote
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#birds" )
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.