Learn how to use jquery. AutoComplete

Source: Internet
Author: User

Eva is used to return data in JSON format

 

Array Implementation AutoComplete function similar to Baidu search box
Test. aspx in the background returns a string to the foreground
VaR DATA = "core | selectors | attributes | traversing | manipulation | CSS | events effects | Ajax utilities ";
Response. Clear ();
Response. Write (data );
Response. End ();
Use the split () method to convert data to an array. Add a text box with ID txtnamecn to the page.
<SCRIPT src = "http://code.jquery.com/jquery-latest.js"> </SCRIPT>
<LINK rel = "stylesheet" href = "http://dev.jquery.com/view/trunk/plugins/autocomplete/demo/main.css" type = "text/CSS"/>
<LINK rel = "stylesheet" href = "http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type = "text/CSS"/>
<SCRIPT type = "text/JavaScript" src = "http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"> </SCRIPT>
<SCRIPT>
$ (Document). Ready (function (){

// Use ajax to request background data
$. Ajax ({type: "Get", contenttype: "application/JSON", URL: "test. aspx", success: function (data ){
VaR arr = data. Split ("|"); // use | as the dividing line to convert a string to an array
$ ("# Txtnamecn"). AutoComplete (ARR );
}});
// $ ("# Txtnamecn"). AutoComplete (ARR );
});
</SCRIPT> //////////////////////////////////// /// // The AutoComplete function is returned in the background using a JSON object JSON object [{"namecn ": "Mo", "nameen": "Glory mercy", "IMO": "9486568" },{ "namecn": "oriental 606", "nameen": "dongfang606 ", "IMO": "1061"}] Because jquery recognizes a JSON object as a string, you can use the eval () method to convert it to the eval function built in JavaScript, to convert a JSON string to a JS object, use a pair of "()" to enclose the string first.
Foreground <SCRIPT>
$ (Document). Ready (function (){
$. Ajax ({
Type: "Get ",
Contenttype: "application/JSON ",
URL: "returndata. aspx ",
Success: function (data ){
VaR DATA = eval ("(" + Data + ")");
$ ("# Txtnamecn"). AutoComplete (data ,{
Minchars: 1,
Matchcontains: True,
AutoFill: false,
Formatitem: function (row, I, max ){
Return I + "/" + MAX + ": \" "+ row. namecn;
},
Formatmatch: function (row, I, max ){
Return row. namecn;
},
Formatresult: function (ROW ){
Return row. namecn;
}
});
$ ("# Txtnameen"). AutoComplete (data ,{
Minchars: 1,
Matchcontains: True,
AutoFill: false,
Formatitem: function (row, I, max ){
Return I + "/" + MAX + ": \" "+ row. nameen;
},
Formatmatch: function (row, I, max ){
Return row. nameen;
},
Formatresult: function (ROW ){
Return row. nameen;
}
});
$ ("# Txtimo"). AutoComplete (data ,{
Minchars: 1,
Matchcontains: True,
AutoFill: false,
Formatitem: function (row, I, max ){
Return I + "/" + MAX + ": \" "+ row. IMO;
},
Formatmatch: function (row, I, max ){
Return row. IMO;
},
Formatresult: function (ROW ){
Return row. IMO;
}
});
}
})
})
</SCRIPT>

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.