Problem with jQuery autocomplete plugin

My project has a requirement to provide an autocomplete function for a text box similar to the one recently used by Google. I need to get data every time I press a key, so I call the jQuery function when I press a key. The problem is that the autocomplete function is launched by clicking the mouse in the text field, and not by pressing a key. I will attach a code snippet to better understand the problem, which is as follows

$(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
    }); 
}); 

Now, what happens when any key is pressed, the alerts work fine, but the second half of the function, i.e.

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

, . , , "cacheLength: 0", , , , - , .

+3
3

, .

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

, JQuery plugin . , Ajax.

, cacheLength 0, docs >= 1. , , ?

1: , , , matchContains: true , cacheLength > 1 (cacheLength = 1 , - cacheLength = 10).

+2

Try to cancel the click event from the text field to which you connected your autorun to:

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

Source: https://habr.com/ru/post/1709836/


All Articles