Use JavaScript to imitate IE's automatic completion of a form _javascript skills like automatic finish success

Source: Internet
Author: User
Tags eval
Recently in writing a JavaScript framework, see the Internet has a lot of automatic completion of the form, so on a whim, with JavaScript wrote a, to add a bit of color to their frame.

Steps:
1. Pass in two parameters, the first one is the form object you want to bind, and the second is the array you want to retrieve.
2. Dynamically create a div as you want to automatically complete the layer, set properties and events (I do not set the div visible and display properties here, but its left set to " -1000px", so that it moved out of the browser, to achieve a hidden effect.
3. Retrieves the incoming array, finds the item that matches or approximates the input, and stores it in a new array. Here is a regular expression of four iterations, write bad, also hope forgive.
4. Process the new array that is stored in the retrieved data, remove the duplicate items, and write them into the Div.
5. Set the div left,top,width.

look at the complete code given below:
Copy Code code as follows:

if (!SX)
var sx={};
sx.activex={};
sx.activex.autocomplete={
Bind:function (a,s) {
var d=document.createelement ("div");
d.style.position= "Absolute";
d.flag= "AutoComplete";
Document.body.appendChild (d);
D.style.left= " -1000px";
D.style.height= "100px";
d.style.overflow= "Auto";
A.onblur=function () {
if (Document.elementfrompoint (window.event.clientx,window.event.clienty). flag== "AutoComplete" | | Document.elementfrompoint (window.event.clientx,window.event.clienty). parentnode.flag== "AutoComplete")
Return
D.innerhtml= "";
D.style.left= " -1000px";
}
A.onkeyup=function () {
if (a.value== "") {
D.innerhtml= "";
Return
}
D.innerhtml= "";
var t=[];
for (var i in s) {
if (eval ("/^" +a.value+ "$/i"). Test (S[i)) {
T.push (S[i]);
}
}
for (var i in s) {
if (eval ("/^" +a.value+ ". +$/i"). Test (S[i)) {
T.push (S[i]);
}
}
for (var i in s) {
if (eval ("/^.+" +a.value+ "$/i"). Test (S[i)) {
T.push (S[i]);
}
}
for (var i in s) {
if (eval ("/^.+" +a.value+ ". +$/i"). Test (S[i)) {
T.push (S[i]);
}
}
for (Var e=0;e<=t.length-2;e++) {
for (Var e1=e+1;e1<=t.length-1;e1++) {
if (T[e]==t[e1]) {
for (Var e2=e1+1;e2<t.length;e2++) {
if (T[e2]) {
T[E2-1]=T[E2];
}
}
T.length=t.length-1;
}
}
}
Alert (t);
for (var i in t) {
var p=document.createelement ("div");
P.innertext=t[i];
P.onmouseenter=function () {
This.style.backgroundcolor= "Blue";
}
P.onmouseleave=function () {
this.style.backgroundcolor= "White";
}
P.onclick=function () {
A.value=this.innertext;
D.style.left= " -1000px";
}
D.appendchild (P)
}
d.style.top=a.offsettop+a.offsetheight+ "px";
d.style.left=a.offsetleft+ "px";
d.style.width=a.offsetwidth+ "px";
}
}
}.

The called HTML code:
Copy Code code as follows:

<title>untitled document</title>
<body>
<script src= "Kongjian.js" ></script>
<input type= "text" size= "a" id= "a" >
<input type= "text" size= "id=" A1 ">
<script>
Sx.activex.autocomplete.bind (document.getElementById ("a"), ["ASD", "a", "sad", "er", "ewrew", "AADASD", "WQQWRQW", " Asasf "," QWEQW "]);
Sx.activex.autocomplete.bind (document.getElementById ("A1"), ["ASD", "a", "sad", "er", "ewrew", "AADASD", "WQQWRQW", " Asasf "," QWEQW "]);
</script>
</body>

Code is not optimized, but also please forgive, here gives a train of thought, let you laughed at.
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.