-1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>autoomplete</title>
<style>
.hidden1
{
margin: 10px;
padding: 10px; 
border: 2px solid  #00CC99; 
width: 200px;
border-radius: 5px;
}
</style>
</head>
<script> 
function showlist() 
{ 
if(!((/^\s*$/).test(edit1.value)))   
{ 
document.getElementById("dropdown").style.display="block";   
var tex=edit1.value; 
var name = new Array("python","django framework","mongodb");    
for(var i = 0;i< name.length;i++) 
{ 
if(name[i].indexOf(tex) != -1)   
{ 
add(name[i],name[i]);   
} } } } 
function add(Text,Value) 
{ 
var opt = document.createElement("option");  
listbox1.options.add(opt); 
opt.text = Text; 
opt.value = Value; 
} 
function selval(listval) 
{ 
edit1.value = listval.value; 
listval.style.display="none";
} 
</script> <body>
<div > 
<center>Autocomplete:<input type="text" id="edit1" name="edit1" onkeyup="showlist()"> </center>
</div> 
<div id="dropdown" style="display:none" align="center" > 
<select id="listbox1" size="3" name="listbox1" onclick="selval(this)" class="hidden1"> 
</select> </div> </body></html>

伙计们,我使用 javascript 创建了一个自动完成文本字段。在浏览器中,只要传递匹配的字符串,就会出现自动提示框,但是每当我按下退格键时,onkeyup 就没有触发并且无法获得我创建的自动提示。请建议一个我提前解决这个问题的方法!

4

1 回答 1

0

XHTML

onkeyup="showlist(e)"

JavaScript

function showlist(e)
{//alert('e.keyCode = '+e.keyCode);
 var e = evt || event;
 var key = e.which || e.keyCode;

 switch (key)
 {
  case 8://Backspace
  alert('backspace pressed.');
  break;
 }
}
于 2013-09-17T11:02:56.570 回答