JQuery-based Automatic plug-in

Source: Internet
Author: User

The following is the html test code: Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> autoComplete </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Style type = "text/css">
. AutoComplete {margin: 8px; position: relative; float: left ;}
. AutoComplete input {width: 200px; height: 25px; line-height: 25px; margin: 0; padding: 0 ;}
. AutoComplete ul {z-index: 99; padding: 0px; margin: 0px; border: 1px #333 solid; width: 200px; background: white; display: none; position: absolute; left: 0; top: 28px ;}
. AutoComplete li {list-style: none ;}
. AutoComplete li a {display: block; color: #000; text-decoration: none; padding: 1px 0 1px 5px ;}
. AutoComplete li a: hover {color: #000; background: # ccc; border: none ;}
</Style>
<Script type = "text/javascript" src = "../javascript/lib/jquery-1.4.2.min.js"> </script>
<Script type = "text/javascript" src = "../javascript/autoComplete. js"> </script>
</Head>
<Body>
<Div class = "autoComplete"> <input value = "input"/> <ul> <li> </ul> </div>
<Div class = "autoComplete"> <input value = "input"/> <ul> <li> </ul> </div>
<Div class = "autoComplete"> <input value = "input"/> <ul> <li> </ul> </div>
<Div class = "autoComplete"> <input value = "input"/> <ul> <li> </ul> </div>
</Body>
</Html>

Below is the jQuery plug-in:Copy codeThe Code is as follows:; (function ($ ){
/* Plugin */
$. Fn. autoComplete = function (o ){
If (o. ajax) o. ajax = $. extend ({url: '', dataType: 'json', async: true}, o. ajax || {});
O. elemCSS = $. extend ({focus: {'color': '# f00'}, blur: {'color':' # 000'}, o. elemCSS | | {});
O = $. extend ({
Source: null,/* privide an array for match */
Ajax: {},/* provide an ajax conditions, if source is exist this parameter is invalid */
Input: 'input',/* provide the selector of input box */
Popup: 'ul ',/* provide the selector of popup box, it must be a ul element of html */
ElemCSS: {}/* provide the focus and blur css objects of items in the popup box */
}, O | {});
Var handler = (function (){
Var handler = function (e, o) {return new handler. prototype. init (e, o );};
Handler. prototype = {
E: null, o: null, timer: null, show: 0, $ input: null, $ popup: null,
Init: function (e, o ){
This. e = e;
This. o = o;
This. $ input = $ (this. o. input, this. e );
This. $ popup = $ (this. o. popup, this. e );
This. initEvent ();
},
Match: function (quickExpr, value, source ){
For (var I in source ){
If (value. length> 0 & quickExpr.exe c (source [I])! = Null)
This. $ popup. append ('<li> <a href = "javascript:;">' + source [I] + '</a> </li> ');
}
If ($ ('Li A', this. $ popup [0]). length) {this. $ popup. show ();} else {this. $ popup. hide ();}
},
Fetch: function (ajax, search, quickExpr ){
Var that = this;
$. Ajax ({
Url: ajax. url + search,
DataType: ajax. dataType,
Async: ajax. async,
Error: function (data, es, et) {alert ('error ');},
Success: function (data) {that. match (quickExpr, search, data );}
});
},
InitEvent: function (){
Var that = this;
This. $ input. focus (function (){
Var value = this. value, quickExpr = RegExp ('^' + value, 'I'), self = this;
That. timer = setInterval (function (){
If (value! = Self. value ){
Value = self. value;
That.$popup.html ('');
If (value! = ''){
QuickExpr = RegExp ('^' + value );
If (that. o. source) that. match (quickExpr, value, that. o. source );
Else if (that. o. ajax) that. fetch (that. o. ajax, value, quickExpr );
}
}
},200 );
}). Blur (function (){
ClearInterval (that. timer );
Var current =-1;
Var len = that. $ popup. find ("li a"). length-1;
$ ("Li a", that. $ popup [0]). click (function (){
That. $ input [0]. value = $ (this). text ();
That.w.popup.html (''). hide ();
}). Focus (function (){
Current = $ (this). parent (). index ();
Using (this).css (that. o. elemCSS. focus );
}). Blur (function (){
Using (this).css (that. o. elemCSS. blur );
});
$ ("Li a", that. $ popup [0]). keydown (function (event ){
If (event. keyCode = 40 ){
Current ++;
If (current <0) current = len;
If (current> len) current = 0;
That. $ popup. find ("li a"). get (current). focus ();
} Else if (event. keyCode = 38 ){
Current --;
If (current> len) current = 0;
If (current <0) current = len;
That. $ popup. find ("li a"). get (current). focus ();
}
});
}). Keydown (function (event ){
If (event. keyCode = 40 ){
That. $ popup. blur (). find ("li a"). get (0). focus ();
}
});
$ (This. e). hover (function () {that. show = 1 ;}, function () {that. show = 0 ;});
$ (Document). click (function () {if (that. show = 0) {that. $ popup. hide ();}});
}
};
Handler. prototype. init. prototype = handler. prototype;/* JQuery style */
Return handler;
})();
Return this. each (function () {handler (this, o );});
};
/* Invoke */
$ (Document). ready (function (){
$ (". AutoComplete"). autoComplete ({
Source: [7567,956,456,938,],
// Ajax: {url: './php/fetch. php? Search = ', dataType: 'json', async: false },
ElemCSS: {focus: {'color': '#0f0'}, blur: {'color': '# f00 '}}
});
});
/* Conclude */
}) (JQuery );

When an ajax request is called, it returns a json data. I used php to read the database for testing. If necessary, the Code is as follows:Copy codeThe Code is as follows: <? Php
Class DataFetch {
Private $ conn, $ rs;
Function _ construct (){
$ This-> conn = mysql_connect ("localhost", "root", "pwd") or die ("Cant't connect host ~ ");
Mysql_select_db ("studentinfo", $ this-> conn) or die ("Cant't select database ~ ");
}
Public function data_list ($ s ){
$ This-> rs = mysql_query ("select sno from student where sno like '$ s %'") or die ("Can't fetch ~ ");
$ DataList = array ();
While ($ row = mysql_fetch_array ($ this-> rs, MYSQL_ASSOC )){
Array_push ($ dataList, $ row );
}
Return $ dataList;
}
}
$ Search = isset ($ argv [1])? $ Argv [1]:
(Isset ($ _ GET ['search'])? $ _ GET ['search']: '');
$ Fetch = new DataFetch ();
$ Data = $ fetch-> data_list ($ search );
Echo '[';
Foreach ($ data as $ key => $ value ){
Echo "\ n \ t ";
Echo '"'. $ value ['sno']. '"';
If ($ key! = Count ($ data)-1) echo ','; else echo "\ n ";
}
Echo "] \ n ";
?>

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.