每当按下退格键时,onkeyup都不会触发?

时间:2013-09-17 10:09:29

标签: javascript html css onkeyup

<!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都没有触发,无法获得我自己创建的自动提示。请建议我一个克服这个问题的方法 提前谢谢!

1 个答案:

答案 0 :(得分: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;
 }
}