jQuery UI Autocomplete experience sharing_jquery
jQuery UI Autocomplete mainly supports two data formats: string Array and JSON.
There is nothing special about the ordinary Array format, as follows:
For Array in JSON format, it is required to have: label and value attributes, as follows:
The label attribute is used to display in the autocomplete pop-up menu, and the value attribute is the value assigned to the text box after selection.
If one of the attributes is not specified, it will be replaced by the other attribute (i.e. value and label value are the same), as follows:
[{label: "cnblogs"}, {label: "囧月"}]
[{value: "cnblogs"}, {value: "囧月" "}]
If neither label nor value is specified, it cannot be used for autocomplete prompts.
Also note that the JSON key output from the server must be in double quotes, as follows:
Otherwise a parsererror error may occur.
Main parameters
Commonly used parameters of jQuery UI Autocomplete are:
Source: used to specify the data source, type is String, Array, Function
String: server-side address used for ajax request, returned Array/JSON format
Array: i.e. string array or JSON array
Function(request, response): Get the input value through request.term, response([Array]) to present the data; (JSONP is this Method)
minLength: When the length of the string in the input box reaches minLength, activate Autocomplete
autoFocus: When the Autocomplete selection menu pops up, automatically select the first one
delay: how many milliseconds to delay activating Autocomplete
Others that are not commonly used will not be listed.
Usage
Suppose there is the following input box on the page:
AJAX request
By specifying the source as the server-side address To implement, as follows:
$("#autocomp") .autocomplete({
source: "remote.ashx",
minLength: 2
});
Then receive it on the server side and output the corresponding results. Please pay attention to the default delivery The parameter name is term:
public void ProcessRequest(HttpContext context )
{
// The query parameter name defaults to term
string query = context.Request.QueryString["term"];
context.Response.ContentType = "text/javascript";
//Output string array or JSON array
context.Response.Write("[{"label":"blog garden","value":"cnblogs"},{"label":"囧月" ,"value":"囧月"}]");
}
Local Array/JSON array
// Local string array
var availableTags = [
"C#",
"C ",
"Java",
"JavaScript",
"ASP",
"ASP.NET",
"JSP",
"PHP",
"Python",
"Ruby"
];
$("#local1").autocomplete({
source: availableTags
});
// Local json array
var availableTagsJSON = [
{ label: "C# Language", value: "C#" },
{ label: "C Language", value: "C " },
{ label: "Java Language", value: " Java" },
{ label: "JavaScript Language", value: "JavaScript" },
{ label: "ASP.NET", value: "ASP.NET" },
{ label: " JSP", value: "JSP" },
{ label: "PHP", value: "PHP" },
{ label: "Python", value: "Python" },
{ label: "Ruby", value: "Ruby" }
];
$("#local2").autocomplete({
source: availableTagsJSON
});
Callback Function method
Acquire custom data by specifying the source as a custom function. The function mainly has 2 parameters (request, response), which are used to obtain the input value, Present results
Get data in local Array mode (imitate Sina Weibo login)
var hosts = ["gmail.com", "live.com", "hotmail.com", "yahoo.com", "cnblogs.com", "Mars.com", "囧月. com"];
$("#email1").autocomplete({
autoFocus: true,
source: function(request, response) {
var term = request.term, //request .term is the input string
ix = term.indexOf("@"),
name = term, // Username
host = "", // Domain name
result = [] ; // result
result.push(term);
// result.push({ label: term, value: term }); // json format
if (ix > -1) {
name = term.slice(0, ix);
host = term.slice(ix 1);
}
if (name) {
var foundHosts = (host ? $. grep(hosts, function(value) {
return value.indexOf(host) > -1;
}) : hosts),
findedResults = $.map(findedHosts, function(value) {
return name "@" value; //Return string format
// return { label: name " @ " value, value: name "@" value }; // json format
});
result = result.concat($.makeArray(findedResults));
}
response(result);//Present the results
}
});
Get data via JSONP
Get it directly from the official DEMO, send an ajax request to the remote server, then process the return result, and finally present it through response:
$("#jsonp").autocomplete({
source: function(request , response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass : "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($ .map(data.geonames, function(item) {
return {
label: item.name (item.adminName1 ? ", " item.adminName1 : "") ", " item.countryName,
value: item.name
}
}));
}
});
},
minLength: 2
});
Main events
jQuery UI Autocomplete has some events that can be used for additional control at some stages:
create(event, ui): When Autocomplete is created, you can use this event in , some control over the appearance
search(event, ui): Before starting the request, you can return false in this event to cancel the request
open(event, ui): When Autocomplete's result list pops up
focus(event, ui): When any item in Autocomplete's result list gets focus, ui.item is the item that gets focus
select(event, ui): When any item in Autocomplete's result list is selected, ui.item is Selected item
close(event, ui): When the result list of Autocomplete is closed
change(event, ui): When the value changes, ui.item is the selected item
The ui parameters of these events The item attribute (if any) has label and value attributes by default, regardless of whether the data set in the source is an Array or a JSON array, as follows:
["cnblogs","blog garden","囧月"]
[{label: "blog garden", value: " cnblogs"}, {label: "囧月", value: "囧月"}]
[{label: "囧月园", value: "cnblogs", id: "1"}, {label: "囧月" month", value: "囧月", id: "2"}]
If it is the third type, you can also get the value of ui.item.id.
These events can be bound in 2 ways, as follows:
// In parameters
$("#autocomp").autocomplete({
source: availableTags
, select: function(e, ui) {
alert(ui.item .value)
}
});
// Bind through bind
$("#autocomp").bind("autocompleteselect", function(e, ui) {
alert(ui.item.value);
});
The event name used by binding through bind is "autocomplete". The event name, such as "select" is "autocompleteselect".
Autocomplete for multiple values
Under normal circumstances, the autocomplete of the input box only requires one value (such as: javascript); if multiple values are needed (such as: javascript, c#, asp.net), you need Bind some events for additional processing:
Return false in the focus event to prevent the value of the input box from being replaced by a single value of autocomplete
Combine multiple values in the select event
Do it in the keydown event of the element Some processing, the reason is the same as 1
Use callback function source to get the last input value and present the result
Or just take the official DEMO code directly:
// Separate multiple values by commas
function split(val) {
return val.split( /,s*/);
}
//Extract the last value of the input
function extractLast(term) {
return split(term).pop();
}
// When pressing the Tab key, cancel setting the value for the input box
function keyDown(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this). data("autocomplete").menu.active) {
event.preventDefault();
}
}
var options = {
// Get focus
focus: function( ) {
// prevent value inserted on focus
return false;
},
// When selecting a value from the autocomplete pop-up menu, add it to the end of the input box and separate it with commas
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms .join(", ");
return false;
}
};
// Multiple values, local array
$("#local3").bind("keydown" , keyDown)
.autocomplete($.extend(options, {
minLength: 2,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
}
}));
// Multiple values, ajax returns json
$("#ajax3").bind("keydown", keyDown)
.autocomplete($.extend(options, {
minLength: 2,
source: function(request, response) {
$.getJSON("remoteJSON.ashx", {
term: extractLast(request.term)
}, response);
}
}));
End
Finally, put the code: Click to download.
For more information, please see the jQuery UI Autocomplete official demo: http://jqueryui.com/demos/autocomplete

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Elden's Ring, the UI page of this game will be automatically hidden after a period of time. Many players do not know how the UI is always displayed. Players can select the gauge display configuration in the display and sound configuration. Click to turn it on. Why does the Elden Ring UI keep displaying? 1. First, after we enter the main menu, click [System Configuration]. 2. In the [Display and Sound Configuration] interface, select the meter display configuration. 3. Click Enable to complete.

Vue is a popular JavaScript framework that uses a component-based approach to build web applications. In the Vue ecosystem, there are many UI component libraries that can help you quickly build beautiful interfaces and provide rich functions and interactive effects. In this article, we will introduce some common VueUI component libraries. ElementUIElementUI is a Vue component library developed by the Ele.me team. It provides developers with a set of elegant,

For AI, "playing with mobile phones" is not an easy task. Just identifying various user interfaces (UI) is a big problem: not only must the type of each component be identified, but also the symbols used , position to determine the function of the component. Understanding the UI of mobile devices can help realize various human-computer interaction tasks, such as UI automation. Previous work on mobile UI modeling usually relies on the view hierarchy information of the screen, directly utilizing the structural data of the UI, and thereby bypassing the problem of identifying components starting from the screen pixels. However, view hierarchy is not available in all scenarios. This method usually outputs erroneous results due to missing object descriptions or misplaced structural information, so even if you use

The jQuery mobile UI framework is a tool for developing mobile applications. It provides rich interface components and interactive effects, allowing developers to quickly build excellent mobile user interfaces. In this article, we will explore some of the most popular jQuery mobile UI frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.jQueryMobiljQueryMobile is an open source mobile UI framework based on HTML5 and CSS3.

UI is the abbreviation of "User Interface", which is mainly used to describe the human-computer interaction, operation logic and beautiful interface of the software. The purpose of UI design is to make software operation easier and more comfortable, and fully reflect its positioning and characteristics. Common UI designs are divided into physical UI and virtual UI, among which virtual UI is widely used in the Internet field.

A few days ago, Google officially pushed the Android 15 Beta 4 update to eligible Pixel smartphone and tablet users. This marks that the Android 15 operating system has entered the platform stable stage, indicating that its stable version will be officially released with global users in the next few days. Meet. At the same time, this development also injects new vitality into Samsung Electronics' Galaxy device series to accelerate the development process of its OneUI7.0 version. 1.[Android15Beta4 promotes Samsung OneUI7.0 stable build](https://www.cnbeta.com/articles/tech/1427022.htm) With Android15Bet

The difference between ux and ui design: 1. UX makes the interface easier to use, and UI makes the interface more beautiful; 2. UX allows users to achieve their goals, and UI makes the interface enhance the brand sense; 3. UX core goals guide users to complete tasks, UI does not; 4. The deliverables of UI and UX are different. The output of UX includes UX experience report, function definition, function planning, project progress, etc., while the output of UI includes visual and interaction, visual design, brand design, motion design, and component design. and design language, etc.

UI, the full name of user interface, refers to the design of human-computer interaction, operation logic and beautiful interface in software. It is divided into physical UI and virtual UI, among which virtual UI is widely used in mobile Internet. Good UI design not only makes the software look tasteful, but more importantly, makes software operation comfortable and easy, fully reflecting the software's positioning and characteristics.
