2009-06-05 139 views
3

我的項目中有一項要求,即爲最近在Google上應用的文本框提供自動完成功能。我需要獲取每個按鍵的數據,所以我在按鍵上調用了一個jQuery函數。問題是自動完成功能會在鼠標單擊文本框時觸發,而不是按鍵。我會附上代碼段爲更好地瞭解哪些是這樣jQuery自動完成插件問題

$(document).keypress(function(){ 
    lastKey = String.fromCharCode(window.event.keyCode); 
    alert('lastKey :: ' + lastKey); 
    var txtVal = document.frm.selectedTechParamName.value + lastKey; 
    alert('txtVal :: ' + txtVal); 
    $("#suggestTechParamName").autocomplete('/AEA-Authoring/TechnicianParameterAutocomplete?userAction=getTechParamsForSvcLvlDataID&txtVal=' + txtVal, { 
     matchContains: true, 
     minChars: 0, 
     cacheLength:0, 
     maxItemsToShow:10 
    }); 
}); 

現在,當按下任意鍵警報工作正常,但發生了什麼事是問題的功能下半年即

$("#suggestTechParamName").autocomplete('/AEA-Authoring/TechnicianParameterAutocomplete?userAction=getTechParamsForSvcLvlDataID&txtVal=' + txtVal, { 
    matchContains: true, 
    minChars: 0, 
    cacheLength:0, 
    maxItemsToShow:10 
}); 

當我們點擊文本框時被調用。同樣,你可以看到我寫的屬性「cacheLength:0」,因爲我不希望自動完成來緩存任何數據,但這看起來似乎不起作用。任何快速回復,將不勝感激。

+0

嗨 - 你可以編輯你的問題來標記你的代碼作爲代碼,所以它不那麼headachy? – karim79 2009-06-05 09:09:50

回答

1

我用這個,事情對我來說工作得很好。

$(document).ready(function() { 
    $("#suggestTechParamName").autocomplete('/AEA-Authoring/TechnicianParameterAutocomplete', { 
     extraParams: { userAction: 'getTechParamsForSvcLvlDataID' 
        } 
    }); 
}); 
1

嘗試從您附上您的autocompleter到文本框解除綁定click事件:

$("#suggestTechParamName").autocomplete('/AEA-Authoring/TechnicianParameterAutocomplete?userAction=getTechParamsForSvcLvlDataID&txtVal=' + txtVal, { 
    matchContains: true, 
    minChars: 0, cacheLength:0, maxItemsToShow:10 
}).unbind("click"); 
+0

嗨! 當我試圖解除綁定click事件時,所有東西都停止工作。 有什麼建議嗎? – 2009-06-05 09:58:31

2

看起來你正在使用的JQuery plugin來實現這一目標。我使用相同的插件,每次按下某個鍵時,都會向服務器發送一個新的Ajax請求。

您提到您正在將cacheLength設置爲0,根據docs,該值必須爲>= 1。你有沒有嘗試改變這個看看它是否改變了行爲?

編輯1:此外,根據文檔,它如果你有cacheLength > 1看來,matchContains: true是唯一有用的(cacheLength = 1意味着沒有緩存,默認爲cacheLength = 10)。