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;
}
}
}
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;
}
}
}
解决方案 »
- 关于JQuery或JS 让显示IE全屏,隐藏IE工具栏
- 请问有没有什么方法可以修改提示框的样式?
- 如何用JavaScript检测是否安装了JAVA虚拟机?同时要检测虚拟机的版本?针对版本的检测主要是检测哪里可以判断不同版本?
- c#.net 2005 asp.net 开发。在javascript中取消按键事件
- JS里的clientY问题
- 用复选框(checkbox)来控制文本框(textfield)的属性
- 关于file选择文件的问题
- "为空或不是对象问题"大家帮忙看看
- dtree放到哪个位置
- 页面上有两个表格,我只需要打印其中一个,怎么办?
- 如何实现:在页面中点击右键时出现像右键菜单一样的层(该层中是一张图片),再点左键又消失?
- 如何用程序设置ie中的打印预览中的设置项?
<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>
1、必须得非常连续快速正确地输入要选择的值,稍慢一点或不连续都不行。怎么来延长缓冲时间呢???
2、假设选择菜单里有个值为:"H102452PLFW",现在要选择它,若敲错了其中一个字符,则会转到这个以这个敲错了的字符为首的值中去,所以必须重新再敲入。请再关注帮忙,谢谢
<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>
把这个值改大就好了
另外可不可以控制缓冲时间?
<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>
<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>
我不知道怎么“//根据自己的需要,增加特殊符号的处理”
那些常用字符的keycode是多少,比如“/”的应该加一个怎样的else if
<body onkeydown="window.status=(event.keyCode)">
你便可以在状态栏中看见键码了
为了增加对符号“/”的处理,
我加了 else if(keyCode==191)sel+="/",但是无效为什么对符号“-”的处理有两个keycode值?
即:else if(keyCode==189||keyCode==109)sel+="-"顺便请帮忙给出对符号“/”的处理应该怎么写,
就此结束
<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>