Home Web Front-end JS Tutorial Implementing autocomplete to automatically complete forms based on bootstrap plug-in_javascript skills

Implementing autocomplete to automatically complete forms based on bootstrap plug-in_javascript skills

May 16, 2016 pm 03:01 PM
bootstrap plug-in automatic completion form

Based on the bootstrap plug-in, autocomplete automatically completes the form, providing script code, use cases, and backend server (php). There are some things that are not clear in the original text. I hope it can help everyone.

First of all, you must load bootstrap&jquery. What needs additional explanation is that the two-dimensional array returned by the backend should be consistent with the call below the formatItem method;
In addition, the returned data must be parseJSON first! Remember.

Related parameter description:

source: function(query,process){}. query represents the string in the current text input box. In this method, you can request data (json object in the form of an array) from the background through ajax, and then use the returned object as a parameter of the process;
formatItem: function(item){}. Convert a specific json object of the returned data into a string format to be displayed in the prompt list. Return value: string;
setValue: function(item){}. When an item in the prompt list is selected, set the value displayed in the text input box and the actual value to be obtained. Return value format: {'data-value':item["The item attribute of the value displayed in the input box"],'real-value':item["The item attribute of the actual value that needs to be obtained"]}, which can be used later through the text input box The real-value attribute gets the value;
items: The maximum number of result sets for auto-complete prompts, default: 8;
minLength: Matching will only be performed when the string in the current text input box reaches this attribute value. Default: 1;
delay: Specify the number of milliseconds before actually requesting data from the background to prevent frequent requests to the background caused by too fast input. Default: 500

Implement autocomplete to automatically complete the form based on bootstrap plug-in, the code is as follows

1. Code

<script>
$('#sim_iccid').autocomplete({
 source:function(query,process){
  var matchCount = this.options.items;//允许返回结果集最大数量
  $.get("http://www.soyiyuan.com/update/",{"iccid":query,"matchCount":matchCount},function(respData){
   respData = $.parseJSON(respData);//解析返回的数据
   return process(respData);
  });
 },
 formatItem:function(item){
  return item["iccid"]+"("+item["mobile"]+")";
 },
 setValue:function(item){
  return {'data-value':item["iccid"],'real-value':item["mobile"]};
 }
});
</script>
Copy after login

2. $data is a two-dimensional array
echo json_encode( $data )
3. Standard json format that needs to be returned

[code][{"iccid":"12345678901234567890","mobile":"1850000"},{"iccid":"12345785","mobile":"1850001"}][code]

Bootstrap autocomplete control Autocomplete is based on bootstrap's own control typeahead, because typeahead does not support complex objects.

//示例代码如下:
 
$('#autocompleteInput').autocomplete({
  source:function(query,process){
   var matchCount = this.options.items;//返回结果集最大数量
   $.post("/bootstrap/region",{"matchInfo":query,"matchCount":matchCount},function(respData){
    return process(respData);
   });
  },
  formatItem:function(item){
   return item["regionName"]+"("+item["regionNameEn"]+","+item["regionShortnameEn"]+") - "+item["regionCode"];
  },
  setValue:function(item){
   return {'data-value':item["regionName"],'real-value':item["regionCode"]};
  }
 });
 
$("#goBtn").click(function(){ //获取文本框的实际值
  var regionCode = $("#autocompleteInput").attr("real-value") || "";
  alert(regionCode);
 });
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the reason why Bootstrap Table displays garbled code What is the reason why Bootstrap Table displays garbled code Apr 07, 2025 am 11:30 AM

The main reasons for displaying garbled code on Bootstrap Table are character set mismatch, encoding problems and poor browser compatibility. Solutions include: 1. Confirm character set consistency; 2. Check data transmission encoding; 3. Replace a browser with better compatibility; 4. Update the Bootstrap Table version; 5. Confirm the data format is correct; 6. Clear the browser cache.

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

How to check the data source encoding of Bootstrap Table How to check the data source encoding of Bootstrap Table Apr 07, 2025 am 11:45 AM

Check and fix Bootstrap Table data source encoding by following these steps: Get the data source and convert it to a string. Use encodeURIComponent() to check the encoding; if it is different, it means that the encoding is not correctly encoded. Use encodeURI() to encode the data source and load it into the Bootstrap Table.

How to write a carousel picture on bootstrap How to write a carousel picture on bootstrap Apr 07, 2025 pm 12:54 PM

Creating a carousel chart using Bootstrap requires the following steps: Create a container containing a carousel chart, using the carousel class. Add a carousel image to the container, using the carousel-item class and the active class (only for the first image). Add control buttons, using the carousel-control-prev and carousel-control-next classes. Add a carousel-indicators metric (small dots), using the carousel-indicators class (optional). Set up automatic playback and add data-bs-ride="carousel&"on the carousel" container.

What list styles does Bootstrap support? What list styles does Bootstrap support? Apr 07, 2025 am 11:15 AM

Bootstrap supports four list styles: unstyled lists, ordered lists, unordered lists (all are default styles), and inline lists that can be used to create horizontal navigation menus and tag clouds. In addition, Bootstrap also provides a powerful list-group class that creates lists with rounded corners, borders, and background colors for displaying project lists or navigation menus.

How to build a bootstrap framework How to build a bootstrap framework Apr 07, 2025 pm 12:57 PM

To create a Bootstrap framework, follow these steps: Install Bootstrap via CDN or install a local copy. Create an HTML document and link Bootstrap CSS to the &lt;head&gt; section. Add Bootstrap JavaScript file to the &lt;body&gt; section. Use the Bootstrap component and customize the stylesheet to suit your needs.

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

How to change the size of a Bootstrap list? How to change the size of a Bootstrap list? Apr 07, 2025 am 10:45 AM

The size of a Bootstrap list depends on the size of the container that contains the list, not the list itself. Using Bootstrap's grid system or Flexbox can control the size of the container, thereby indirectly resizing the list items.

See all articles