PHP+jQuery implements automatic completion function source code_PHP tutorial

WBOY
Release: 2016-07-21 15:10:02
Original
1075 people have browsed it

I manually wrote a drop-down auto-completion function earlier. It is simple to write and only implements the mouse selection function and does not support keyboard selection. Since this function is used in many places in the project, it needs to be done carefully. It is found that the function of the select2 plug-in can meet the current needs.

I encountered some doubts when using the jquery plug-in select2. Whether it is passing through json data or fetching data through jsonp, it can be returned correctly. However, the items in the drop-down list cannot be selected, and neither mouse nor keyboard selection is valid.

Later I discovered that the select2 plug-in implements selection based on the id field in the data. So whether it is json or jsonp, the data returned by ajax must have an id field. If such an ID does not exist in the actual database, you can also construct one manually, but the uniqueness of the ID must be ensured.

Here is the source code of the template file try_diy.tpl:
The column input box is where the plug-in works, but the value returned is the id, which we need after the page is submitted Re-present the section selected by the user to the user. My approach is to query the corresponding section name based on the id submitted in the form. When the controller receives the id value and it is not empty, the name value corresponding to the section id is presented to the user at the same time. displayed on the page. Since the select2 plug-in puts the name in the inner span element of the constructed

, I will write the name value of the hidden field into span element.

Copy code The code is as follows:







Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!