2009-06-05 128 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)。