var strKey;
var lasttimer=Date.parse(Date());
var timelimit=2000;    
function onKeyPress()
{
 if(strKey==null)
 {
  strKey="";
 }
 var length;
 var strText;
 var timer;
 var TheForm;
 TheForm=event.srcElement;
 timer=Date.parse(Date());
 if(timer-lasttimer>timelimit)
  {strKey="";}
 lasttimer=timer;
 strKey=strKey + String.fromCharCode(window.event.keyCode); 
 length=TheForm.length;
 for(i=0;i<length;i++)
 {
  strText=TheForm.options[i].text;
  strText=strText.toLowerCase();
  intI=strText.indexOf(strKey);
  if (intI==0)
  {
   TheForm.options[i].selected=true;
   window.event.returnValue=false;
   return;
  }   
 } 
}

解决方案 »

  1.   

    输入名字的首字缩写即可找到,如张三火(zsh)<select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
         <option value="ZS" >张三</option>
         <option value="ZSH">张三火</option>
         <option value="LS">李四</option>
         <option value="LSS">李四水</option>
    </select><script>
    var sel="",timer=null;
    function spellList(){
    /********(qiushuiwuhen 2002-9-20)***********/
           with(window.event){
              with(srcElement){
                  if(keyCode<48)return;
                  if(keyCode>95)keyCode-=48
                  sel+=String.fromCharCode(keyCode)
                  window.status=sel
                  for(i=0;i<length;i++){
                   if(options[i].value.indexOf(sel)==0){selectedIndex=i;break;}
                  }
              }
              returnValue=false;
              clearTimeout(timer)
              timer=setTimeout("sel=''",500);
         }
    }
    </script>
      

  2.   

    qiushuiwuhen(秋水无恨) 的这个程式(在ASP专栏里已经回复我了)只对汉字有用。(不过这种思想真的很好,可不可以给我个支持 ‘选择值里只含字母或数字等其他常用字符’ 的脚本) junguo(junguo)的程式我有两点疑问:
    1、必须得非常连续快速正确地输入要选择的值,稍慢一点或不连续都不行。怎么来延长缓冲时间呢???
    2、假设选择菜单里有个值为:"H102452PLFW",现在要选择它,若敲错了其中一个字符,则会转到这个以这个敲错了的字符为首的值中去,所以必须重新再敲入。请再关注帮忙,谢谢
      

  3.   

    同理可做呀,增加了退格键backspace的功能,如果输错,可以退格<select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
         <option>H102452PLFW</option>
         <option>H102453PLFW</option>
         <option>H102352PLFW</option>
         <option>H102354PLFW</option>
    </select><script>
    var sel="",timer=null;
    function spellList(){
    /********(qiushuiwuhen 2002-9-20)***********/
           with(window.event){
              with(srcElement){
                  if(keyCode<48&&keyCode!=8)return;
                  if(keyCode>95)keyCode-=48
                  if(keyCode==8)sel=sel.slice(0,-1)
                  else sel+=String.fromCharCode(keyCode)
                  window.status=sel
                  for(i=0;i<length;i++){
                   if(options[i].text.indexOf(sel)==0){selectedIndex=i;break;}
                  }
              }
              returnValue=false;
              clearTimeout(timer)
              timer=setTimeout("sel=''",1000);
         }
    }
    </script>
      

  4.   

    var timelimit=2000; 
    把这个值改大就好了
      

  5.   

    junguo(junguo) :我把timelimit改大了,但结果反而选不到了,改得小点(我甚至把它设为零)也没什么变化,你程式里面:timer好像等于lasttimer?秋水兄,你的那个程式只对大写字母和数字有效,可不可以不分大小写,而且对减号等几个非常常用的字符也有效?
    另外可不可以控制缓冲时间?
      

  6.   

    <select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
         <option>H102452PLFW</option>
         <option>H102453PLFW</option>
         <option>H102352PLFW</option>
         <option>H102354PLFW</option>
         <option>h202154PLFW</option>
         <option>-102354PLFW</option>
    </select><script>
    var sel="",timer=null,re="";
    function spellList(){
    /********(qiushuiwuhen 2002-9-20)***********/
           with(window.event){
              with(srcElement){
                  if(keyCode<48&&keyCode!=8)return;
                  if(keyCode>95&&keyCode<106)keyCode-=48
                  if(keyCode==8)sel=sel.slice(0,-1)
                  else if(keyCode==189||keyCode==109)sel+="-"
    //根据自己的需要,增加特殊符号的处理
                  else sel+=String.fromCharCode(keyCode)
                  window.status=sel
                  re=eval("/"+sel+"/i")
                  for(i=0;i<length;i++){
                   if(options[i].text.search(re)==0){selectedIndex=i;break;}
                  }
              }
              returnValue=false;
              clearTimeout(timer)
              timer=setTimeout("sel=''",2000);//延长时间
         }
    }
    </script>
      

  7.   

    微软的例子,在select获得焦点的情况下,按相应键跳转到相应字母开始的第一个选项<HTML>
    <HEAD><SCRIPT language=javascript>
    // Auto-select listbox// mike pope, Visual Basic UE// This script and the listbox on this page illustrates one 
    // way to create an "auto-complete" listbox, where thevar toFind = "";              // Variable that acts as keyboard buffer
    var timeoutID = "";           // Process id for timer (used when stopping 
                                  // the timeout)
    timeoutInterval = 250;        // Milliseconds. Shorten to cause keyboard 
                                  // buffer to be cleared faster
    var timeoutCtr = 0;           // Initialization of timer count down
    var timeoutCtrLimit = 3 ;     // Number of times to allow timer to count 
                                  // down
    var oControl = "";            // Maintains a global reference to the 
                                  // control that the user is working with.function listbox_onkeypress(){
       // This function is called when the user presses a key while focus is in 
       // the listbox. It maintains the keyboard buffer.
       // Each time the user presses a key, the timer is restarted. 
       // First, stop the previous timer; this function will restart it.
       window.clearInterval(timeoutID)   // Which control raised the event? We'll need to know which control to 
       // set the selection in.
       oControl = window.event.srcElement;   var keycode = window.event.keyCode;
       if(keycode >= 32 ){
           // What character did the user type?
           var c = String.fromCharCode(keycode);
           c = c.toUpperCase(); 
           // Convert it to uppercase so that comparisons don't fail
           toFind += c ; // Add to the keyboard buffer
           find();    // Search the listbox
           timeoutID = window.setInterval("idle()", timeoutInterval);  
           // Restart the timer
        }
    }function listbox_onblur(){
       // This function is called when the user leaves the listbox.   window.clearInterval(timeoutID);
       resetToFind();
    }function idle(){
       // This function is called if the timeout expires. If this is the 
       // third (by default) time that the idle function has been called, 
       // it stops the timer and clears the keyboard buffer   timeoutCtr += 1
       if(timeoutCtr > timeoutCtrLimit){
          resetToFind();
          timeoutCtr = 0;
          window.clearInterval(timeoutID);
       }
    }function resetToFind(){
       toFind = ""
    }
    function find(){
        // Walk through the select list looking for a match    var allOptions = document.all.item(oControl.id);    for (i=0; i < allOptions.length; i++){
           // Gets the next item from the listbox
           nextOptionText = allOptions(i).text.toUpperCase();       // By default, the values in the listbox and as entered by the  
           // user are strings. This causes a string comparison to be made, 
           // which is not correct for numbers (1 < 11 < 2).
           // The following lines coerce numbers into an (internal) number 
           // format so that the subsequent comparison is done as a 
           // number (1 < 2 < 11).       if(!isNaN(nextOptionText) && !isNaN(toFind) ){
                  nextOptionText *= 1;        // coerce into number
                  toFind *= 1;
           }        // Does the next item match exactly what the user typed?
            if(toFind == nextOptionText){
                // OK, we can stop at this option. Set focus here
                oControl.selectedIndex = i;
                window.event.returnValue = false;
                break;
            }        // If the string does not match exactly, find which two entries 
            // it should be between.
            if(i < allOptions.length-1){           // If we are not yet at the last listbox item, see if the 
               // search string comes between the current entry and the next 
               // one. If so, place the selection there.           lookAheadOptionText = allOptions(i+1).text.toUpperCase() ;
               if( (toFind > nextOptionText) && 
                  (toFind < lookAheadOptionText) ){
                  oControl.selectedIndex = i+1;
                  window.event.cancelBubble = true;
                   window.event.returnValue = false;
                  break;
               } // if
               } // if        else{           // If we are at the end of the entries and the search string 
               // is still higher than the entries, select the last entry           if(toFind > nextOptionText){
                   oControl.selectedIndex = allOptions.length-1 // stick it 
                                                                // at the end
                   window.event.cancelBubble = true;
                   window.event.returnValue = false;
                   break;
               } // if
           } // else
        }  // for
    } // function
    </SCRIPT>
    </HEAD>
    <BODY>
    <P><B>Text</B></P>
    <SELECT ID=select1 tabindex=1 width=20 size=7 style="width:125px" 
     onkeypress="listbox_onkeypress()" onblur="listbox_onblur()">
       <OPTION value=2>Abby</OPTION>
       <OPTION value=1>Alice</OPTION>
       <OPTION value=3>Barry</OPTION>
       <OPTION value=3>Beth</OPTION>
       <OPTION value=3>Bobby</OPTION>
       <OPTION value=3>Catherine</OPTION>
       <OPTION value=3>Chuck</OPTION>
       <OPTION value=3>Dave</OPTION>
       <OPTION value=3>Dick</OPTION>
       <OPTION value=3>Dino</OPTION>
       <OPTION value=3>Elmer</OPTION>
       <OPTION value=3>Emily</OPTION>
       <OPTION value=3>Ethel</OPTION>
       <OPTION value=3>William</OPTION>
       <OPTION value=3>Xerxes</OPTION>
       <OPTION value=3>Yolanda</OPTION>
       <OPTION value=3>Zachary</OPTION>
    </SELECT>
    </BODY>
    </HTML>
      

  8.   

    请恕我对Javascript的陌生
    我不知道怎么“//根据自己的需要,增加特殊符号的处理”
    那些常用字符的keycode是多少,比如“/”的应该加一个怎样的else if
      

  9.   

    编写如下的一个html页面
    <body onkeydown="window.status=(event.keyCode)">
    你便可以在状态栏中看见键码了
      

  10.   

    TO freefalcon(心宇):
    为了增加对符号“/”的处理,
    我加了 else if(keyCode==191)sel+="/",但是无效为什么对符号“-”的处理有两个keycode值?
    即:else if(keyCode==189||keyCode==109)sel+="-"顺便请帮忙给出对符号“/”的处理应该怎么写,
    就此结束
      

  11.   

    <select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
         <option>H102452PLFW</option>
         <option>H102453PLFW</option>
         <option>H102352PLFW</option>
         <option>/102354PLFW</option>
         <option>h202154PLFW</option>
         <option>-102354PLFW</option>
    </select><script>
    var sel="",timer=null,re="";
    function spellList(){
    /********(qiushuiwuhen 2002-9-20)***********/
           with(window.event){
              with(srcElement){
                  if(keyCode<48&&keyCode!=8)return;
                  if(keyCode>95&&keyCode<106)keyCode-=48
                  if(keyCode==8)sel=sel.slice(0,-1)
                  else if(keyCode==189||keyCode==109)sel+="-"
                  else if(keyCode==191)sel+="/"
    //根据自己的需要,增加特殊符号的处理
                  else sel+=String.fromCharCode(keyCode)
                  window.status=sel
                  for(i=0;i<length;i++){
                   if(options[i].text.toLowerCase().indexOf(sel.toLowerCase())==0){selectedIndex=i;break;}
                  }
              }
              returnValue=false;
              clearTimeout(timer)
              timer=setTimeout("sel=''",2000);//延长时间
         }
    }
    </script>