如果您使用 jQuery,一个很棒的插件是http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/。只需使用 css 隐藏下拉框,并保留选项卡自动完成功能。
我认为为自己制作一个jquery插件会很简单......
沿着 Listen for the Tab Key 的行当按下 tab 键时,触发 tab:press on input.autotab
$(document).keydown(function(e){ if (e.keyCode = (tab-key?)){
$('input.autotab').trigger('tab:press');
});
将 input.autotab 的 tab:press 事件(在每个循环中......如果 focus==true 等)绑定到 javascript 数组查找或 xhr 请求(ajax),然后将该输入的值设置为返回的数据。
$('input.autotab').bind('tab:press', function(){
if (this.hasFocus){
this.disabled = true;
$.get('/autotab', { q: $(this).val() }, function(response){
$(this).val(response);
this.disabled = false;
}, function(){ this.disabled = false; });
}
});
在您的自动建议脚本中,一旦值在数据库中匹配多次(使用带有索引的 for 循环,在第一个元素匹配的索引元素处停止),然后将值返回到该点。