PHP+jQuery implements automatic completion function source code_PHP tutorial
WBOY
Release: 2016-07-21 15:10:02
Original
1097 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:
<script> <br>$(document).ready(function( ) { <br>$('#colum').select2({ <br>minimumInputLength: 0, <br>placeholder: 'Select section', <br>ajax: { <br>url: "http://pm .feiliu.com/?c=try&a=ajax_diy",<SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">//</SPAN><SPAN style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000"> Provide the url address of the jsonp request</SPAN> <br> dataType: 'jsonp', <br> jsonp: "callback", // passed to the request handler or page, Used to get the jsonp callback function name (usually the default is: callback, so it can be omitted) <br> use using using ’ ’ ’ ’ ’ ’ ’ s ’ through ’ s ’ ‐ ‐ ‐ ‐‐‐‐ Random function name, you can also write "?", jQuery will automatically process the data <br>quietMillis: 100, <br>data: function(name, page) { <br>return { <br>types: ["exercise"] , <br>limit: -1, <br>q: name <br>}; <br>}, <br>results: function(data, page) { <br>return { results: data.items } <br>} <br>}, <br>formatResult: function(exercise) { <br>return "<div class='select2-user-result'>" + exercise.name + "</div>"; <br>}, <br>formatSelection: function(exercise) { <br>return exercise.name; <br>} <br>}); <br><br>$('#colum').change(function (){ <br>frm.submit(); <br>}); <br>var name = $("#columname").val(); <br>if(name){ <br>$(" #s2id_colum").find("span").text(name); <br>} <br>}); <br></script>
public static function get_forum_name_by_fid($fid) { $sql = "SELECT name FROM sq_forums WHERE fid='$fid' ORDER BY threads_counter DESC"; $data = pm_db11::result_first($sql); return $data; } public static function get_ajx_forum_by_tpid($tpid, $fid) { //产品ID:tpid,版块ID:fid $data = array(); if($tpid && $fid){//构造一个id字段,也可以通过对查询结果加工构造 $sql = "SELECT fid AS id,fid,name FROM sq_forums WHERE tpid='$tpid' AND name LIKE N'%$fid%' ORDER BY threads_counter DESC"; $query = pm_db11::query($sql); $data = pm_db11::fetch_all($query); } return $data; }
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