-1;varisIE=navigator.userAgent.indexOf("MSI..." />
Autocomplete Textbox Example javascript实现自动完成成功
Autocomplete Textbox Example javascript实现自动完成成功
发布时间:2016-12-30 来源:查字典编辑
摘要:复制代码代码如下:varisOpera=navigator.userAgent.indexOf("Opera")>-1;varisIE=na...

复制代码 代码如下:

<SCRIPTlanguage=JScripttype=text/javascript>

varisOpera=navigator.userAgent.indexOf("Opera")>-1;

varisIE=navigator.userAgent.indexOf("MSIE")>1&&!isOpera;

varisMoz=navigator.userAgent.indexOf("Mozilla/5.")==0&&!isOpera;

functiontextboxSelect(oTextbox,iStart,iEnd){

switch(arguments.length){

case1:

oTextbox.select();

break;

case2:

iEnd=oTextbox.value.length;

/*fallsthrough*/

case3:

if(isIE){

varoRange=oTextbox.createTextRange();

oRange.moveStart("character",iStart);

oRange.moveEnd("character",-oTextbox.value.length+iEnd);

oRange.select();

}elseif(isMoz){

oTextbox.setSelectionRange(iStart,iEnd);

}

}

oTextbox.focus();

}

/*

functiontextboxReplaceSelect(oTextbox,sText){

if(isIE){

varoRange=oTextbox.createTextRange();

oRange.text=sText;

oRange.collapse(true);

oRange.select();

}elseif(isMoz){

variStart=oTextbox.selectionStart;

oTextbox.value=oTextbox.value.substring(0,iStart)+sText+oTextbox.value.substring(oTextbox.selectionEnd,oTextbox.value.length);

oTextbox.setSelectionRange(iStart+sText.length,iStart+sText.length);

}

oTextbox.focus();

}

*/

functionautocompleteMatch(sText,arrValues){

for(vari=0;i<arrValues.length;i++){

if(arrValues[i].indexOf(sText)==0){

returnarrValues[i];

}

}

returnnull;

}

functionautocomplete(oTextbox,oEvent,arrValues){

switch(oEvent.keyCode){

case38://uparrow

case40://downarrow

case37://leftarrow

case39://rightarrow

case33://pageup

case34://pagedown

case36://home

case35://end

case13://enter

case9://tab

case27://esc

case16://shift

case17://ctrl

case18://alt

case20://capslock

case8://backspace

case46://delete

returntrue;

break;

default:

//下面这一行用处不大(被注释)

//textboxReplaceSelect(oTextbox,isIE?oTextbox.value/*oEvent.keyCode*/:oEvent.charCode);

variLen=oTextbox.value.length;

varsMatch=autocompleteMatch(oTextbox.value,arrValues);

if(sMatch!=null){

oTextbox.value=sMatch;

textboxSelect(oTextbox,iLen,oTextbox.value.length);

}

returnfalse;

}

}

</SCRIPT>

<SCRIPT>

vararrValues=["red","orange","yellow","green","blue","indigo","violet","brown"];

</SCRIPT>

<H2>AutocompleteTextboxExample</H2>

<P>Typeinacolorinlowercase:输入一个以小写字母开头的颜色(英文单词,比如:r、b等)<BR><INPUTid=txt1onkeyup="returnautocomplete(this,event,arrValues)"></P>

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类