AutoComplete Textbox Example JavaScript implements success _javascript skills automatically

Source: Internet
Author: User

Copy Code code as follows:

<script Language=jscript type=text/javascript>
var Isopera = navigator.userAgent.indexOf ("Opera") >-1;
var Isie = navigator.userAgent.indexOf ("MSIE") > 1 &&!isopera;
var Ismoz = navigator.userAgent.indexOf ("MOZILLA/5.") = = 0 &&!isopera;
function Textboxselect (Otextbox, IStart, iend) {
Switch (arguments.length) {
Case 1:
Otextbox.select ();
Break
Case 2:
Iend = OTextbox.value.length;
/* Falls through * *

Case 3:
if (Isie) {
var oRange = Otextbox.createtextrange ();
Orange.movestart ("character", IStart);
Orange.moveend ("character",-otextbox.value.length + iend);
Orange.select ();
else if (Ismoz) {
Otextbox.setselectionrange (IStart, iend);
}
}
Otextbox.focus ();
}
/*
function Textboxreplaceselect (Otextbox, stext) {
if (Isie) {
var oRange = Otextbox.createtextrange ();
Orange.text = Stext;
Orange.collapse (TRUE);
Orange.select ();
else if (Ismoz) {
var iStart = 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 ();
}
*/
function Autocompletematch (stext, arrvalues) {
for (Var i=0 i < arrvalues.length; i++) {
if (Arrvalues[i].indexof (stext) = = 0) {
return arrvalues[i];
}
}
return null;
}
Function AutoComplete (Otextbox, oevent, arrvalues) {
Switch (oevent.keycode) {
Case://up Arrow
Case://down Arrow
Case://left Arrow
Case://right Arrow
Case://page up
Case://page Down
Case://home
Case://end
Case://enter
Case 9://tab
Case://esc
Case://shift
Case://ctrl
Case://alt
Case://caps Lock
Case 8://backspace
Case://delete
return true;
Break
Default
The following line is not very useful (annotated)
Textboxreplaceselect (Otextbox, Isie otextbox.value/*oevent.keycode*/: Oevent.charcode);
var ilen = oTextbox.value.length;
var smatch = Autocompletematch (Otextbox.value, arrvalues);
if (Smatch!= null) {
Otextbox.value = Smatch;
Textboxselect (Otextbox, Ilen, oTextbox.value.length);
}

return false;
}
}
</SCRIPT>
<SCRIPT>
var arrvalues = ["Red", "orange", "Yellow", "green", "Blue", "Indigo", "Violet", "Brown"];
</SCRIPT>
<p>type in a-color in lowercase: Enter a color that starts with a lowercase letter (English words, such as: R, B, etc.) <br><input id=txt1 onkeyup= "return AutoComplete (This, event, arrvalues) "></P>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.