An in-depth analysis of the AutoComplete component of jquery _jquery

Source: Internet
Author: User

Simple example

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>AutocompleteOption</title>
<link rel= "stylesheet" type= "Text/css" href= "Themes/base/jquery.ui.all.css"/>
<script type= "Text/javascript" src= "Js/jquery-1.4.2.min.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.core.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.widget.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.position.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.autocomplete.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
/* Initialize data source/*
var keys = ["JSP", "JavaScript", "jquery", "ASP", "asp.net", "PHP",];

$ (' #searchBox '). AutoComplete ({
Source:keys,
Minlength:2
});
});
</script>
<style>
body{padding:30px;}
</style>
<body>
<input id= "SearchBox"/>
</body>


Effect Chart:

In the code above, wrap the input box on the page as a jquery object when the page is initialized, and then wrap it as a auto-complete component using the AutoComplete () method, initializing its minimum response length options and data source options
2: Ways to Automate components
There are search, Open, focus, Select, close, and change events
Copy Code code as follows:

Function (event, UI) {
Event: Events object When the event is triggered
UI, is the user interface object, Ui.item is an object that contains the label and Value property
}

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>AutocompleteEvent</title>
<link rel= "stylesheet" type= "Text/css" href= "Themes/base/jquery.ui.all.css"/>
<script type= "Text/javascript" src= "Js/jquery-1.4.2.min.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.core.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.widget.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.position.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.ui.autocomplete.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
/*
Initializing a data source
*/
var keys = ["C + +", "C #", "C",
"Java", "ee", "jsp", "JavaScript", "jquery",
"ASP", "asp.net",
"Ruby", "vb.net", "Visual Basic", "VB",
"Photo shop", "PHP",
"Flax", "flash"];
function GetValues (key) {
var ks = [];

if (key = = "") return to KS; Returns an empty collection if the keyword is an empty string

for (var i = 0; i<keys.length; i++) {
if (keys[i].indexof (key) = = 0) {
Ks[ks.length] = keys[i];
}
}
return KS;
}

$ (' #searchBox '). AutoComplete ({
Source: [],
Search:function (Event,ui) {
$ (' #searchBox '). AutoComplete (
"Option", "Source", GetValues ($ (this). Val ())
);
}
});
});
</script>
<style>
body{padding-top:30px;}
td{text-align:center; vertical-align:middle; padding:10px;}
#searchBox, #Search {padding:2px; font-size:15px;}
#searchBox {width:220px;height:17px;}
#Search {width:80px;}
</style>
<body>
<table border= "0" align= "center" >
<tr>
&LT;TD colspan= "2" ></tr>
<tr>
<td><input id= "SearchBox"/></td>
<td><button id= "Search" >Search</button></td>
</tr>
</table>
</body>

Effect Chart:


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.