Code of the jquery auto-search function

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
<Style type = "text/css">
Body {margin: 0px; padding: 0px ;}
Ul {margin: px; padding: 0px; list-style-type: none ;}
</Style>
<Script src = "jquery-1.8/jquery-1.8.0.js"> </script>
<Script>
$ (Function (){
A ();
Onclick ();
$ ("# Txt"). bind ("keyup", function (){
Txtchange (0 );
});

});

Function (){
Ularray = [];
Var data = [{}, {}];

// Input data to ul
Var ul = $ ("# ul1 ");
$. Each (data, function (index, item)
{
Var li = $ ("<li> </li> ");

$. Each (item, function (name, value)
{
Var span = $ ("<span> </span>" 2.16.html (value );
Li. append (span );
Ularray. push (value );
});
Ul. append (li );
});

// Sort
Ularray. sort ();
};

// Keyup event
Function txtchange (flag)
{
Var textObj = $ ("# txt"). val ();
Var divObj = $ ("# div1" pai.html ();
Var array = [];

With (divObj)
{
Var ulHTML = divObj. match (/<[^>] *>/) [0]; // get the start tag
Val = ularray + ""; // convert to string

For (var I = 0; I <ularray. length; I ++)
{
If (val. split (",") [I]. indexOf (textObj )! =-1 | flag) // splits the string into an array.
{
Array [array. length] = "<li> <span>" + ularray [I] + "</span> </li> ";
};
}; // Put the new set into an array

Var liHtml = "";
$. Each (array, function (item, val ){
LiHtml + = val;
}); // Remove the comma between Arrays

DivObj = ulHTML + liHtml + "</ul> ";
$ ("# Ul1" pai.html (divObj );
Onclick (); // click the new array
};
};

// Span Click Event
Function onclick (){
$ ("# Ul1 li span"). click (function ()
{
Var oli = $ (this );
Var otxt00000000(this0000.html ();
$ ("# Txt"). empty (). val (otxt );

});
};
</Script>
</Head>

<Body>
<Center>
<Form>
<Input id = "txt" type = "text"/>
Automatic prompt
<Div id = "div1">
<Ul id = "ul1">
</Ul>
</Div>
</Form>
</Center>
</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.