Use of jquery AutoComplete

Source: Internet
Author: User

I recently started learning jquery and want to automatically display results similar to Google search. So I chose to use the jquery AutoComplete plug-in.

Download the required files, jquery.autocomplete.jsand jquery.autocomplete.css.

Since the data obtained by the control can be obtained through the array and URL methods, I wrote two simple examples to test it.

Front-endCodeAs follows:

 

Code

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " AutoComplete. aspx. CS " Inherits = " AutoComplete "   %>

<! 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 Runat = "Server" >
< Title > </ Title >
< Script SRC = "Scripts/jquery-1.4.1-vsdoc.js" Type = "Text/JavaScript" > </ Script >
< Script SRC = "Scripts/jquery. autocomplete. js" Type = "Text/JavaScript" > </ Script >
< Link Href = "Styles/jquery.autocomplete.css" Type = "Text/CSS"   />
< Script Language = "JavaScript" Type = "Text/JavaScript" >
// Directly obtained from Arrays
$ (Document). Ready (( Function (){
VaR Data = [ " Hebei " , " Henan " , " Shandong Province " , " Beijing " , " Tianjin " ];
$ ( " # Txtprovince " ). AutoComplete (data );
// Obtained by the server
$ ( " # Txtuser " ). AutoComplete ( " GetUserName. aspx " );
}
));
</ Script >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
Province: < Input ID = "Txtprovince" Type = "Text"   />
</ Div >
< Div >
User name: < Input ID = "Txtuser" Type = "Text"   /> </ Div >
</ Form >
</ Body >
</ Html >

 

The user name is used to read data from the background, and the corresponding URL isGetUserName. aspx. The. CS file on this page is:

 

Code

Protected   Void Page_load ( Object Sender, eventargs E)
{
// The Default Input key value is Q.
If (Request. querystring [ " Q " ] ! =   Null )
{
String Key = Request. Params [ " Q " ]. Tostring ();
String Result =   "" ;
DB =   New DB ();
String SQL =   " Select username from users where username like' "   + Key +   " %' " ;
Sqldatareader Dr = DB. getreader (SQL );
While (Dr. Read ())
{
Result + = Dr [ " Username " ]. Tostring () +   " \ N " ;
}
If (Result =   "" )
Result =   " Not exists " ;
Response. Write (result );
}
}

 

After writing, you can find that you can implement the desired functions, but there is always a problem with the style. The displayed result list is ugly, as if CSS was not applied. I thought about the problem for a long time. I suddenly found a low-level mistake this morning. I wrote fewer rel = "stylesheet", my God! The server is ready for you.

This is a simple example. Continue learning.

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.