Ajax basics _ 4.9 Automatic completion

Source: Internet
Author: User

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

<Html>
<Title> Ajax Auto Complete </title>
<Style type = "text/css">
. MouseOut {background: #708090; color: # FFFAFA ;}
. MouseOver {background: # FFFAFA; color: #000000 ;}
</Style> <script type = "text/javascript">
Var xmlHttp;
Var completeDiv;
Var inputField;
Var nameTable;
Var nameTableBody;
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");}
Else if (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}

Function initVars (){
InputField = document. getElementByIdx_x ("names ");
NameTable = document. getElementByIdx_x ("name_table ");
CompleteDiv = document. getElementByIdx_x ("popup ");
NameTableBody = document. getElementByIdx_x ("name_table_body ");
}
Function findNames (){
InitVars ();
If (inputField. value. length> 0 ){
CreateXMLHttpRequest ();
Var url = "AutoCompleteServlet? Names = "+ escape (inputField. value );
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = callback;
XmlHttp. send (null );
} Else {
ClearNames ();
}
}

Function callback (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Var name = xmlHttp. responseXML. getElementsByTagName_r ("name") [0]. firstChild. data;
SetNames (xmlHttp. responseXML. getElementsByTagName_r ("name "));
} Else if (xmlHttp. status = 204 ){
ClearNames ();}}}

Function setNames (the_names ){
ClearNames ();
Var size = the_names.length;
SetOffsets ();
Var row, cell, txtNode;
For (var I = 0; I <size; I ++ ){
Var nextNode = the_names [I]. firstChild. data;
Row = document. createElement_x ("tr ");
Cell = document. createElement_x ("td ");
Cell. onmouseout = function () {this. className = 'mouseover ';};
Cell. onmouseover = function () {this. className = 'mouseout ';};
Cell. setAttribute ("bgcolor", "# FFFAFA ");
Cell. setAttribute ("border", "0 ");
Cell. onclick = function () {populateName (this );};
TxtNode = document. createTextNode (nextNode );
Cell. appendChild_xss (txtNode );
Row. appendChild_xss (cell );
NameTableBody. appendChild_xss (row );
}
}

Function setOffsets (){
Var end = inputField. offsetWidth;
Var left = calculateOffsetLeft (inputField );
Var top = calculateOffsetTop (inputField) + inputField. offsetHeight;
CompleteDiv. style. border = "black 1px solid ";
CompleteDiv. style. left = left + "px ";
CompleteDiv. style. top = top + "px ";
NameTable. style. width = end + "px ";
}

Function calculateOffsetLeft (field ){
Return calculateOffset (field, "offsetLeft ");}

Function calculateOffsetTop (field ){
Return calculateOffset (field, "offsetTop ");}

Function calculateOffset (field, attr ){
Var offset = 0;
While (field ){
Offset + = field [attr];
Field = field. offsetParent;
}
Return offset;
}

Function populateName (cell ){
InputField. value = cell. firstChild. nodeValue;
ClearNames ();}

Function clearNames (){
Var ind = nameTableBody. childNodes. length;
For (var I = ind-1; I> = 0; I --){
NameTableBody. removeChild (nameTableBody. childNodes [I]);
}
CompleteDiv. style. border = "none ";
}

</Script> <H1> Ajax Auto Complete Example <Input type = "text" size = "20" id = "names" onkeyup = "findNames ();" style = "height: 20;"/>
<Div style = "position: absolute;" id = "popup">
<Table id = "name_table" bgcolor = "# FFFAFA" border = "0" cellspacing = "0" cellpadding = "0"/>
<Tbody id = "name_table_body"> </tbody> </table> </div>

</Body>

</Html>

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.