【发布时间】:2015-01-09 16:00:46
【问题描述】:
我正在按照这个链接使用 jquery、php 和 mysql 构建一个多值自动完成系统。例如:如果我在输入字段中输入内容,它将在输入框中显示一个搜索结果,如果搜索另一个术语,它将再次显示另一个结果,该结果将以逗号分隔显示。就像这个链接一样:http://jqueryui.com/autocomplete/#multiple-remote。代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Multiple, remote</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.ui-autocomplete-loading {
background: white url("loading-image.gif") right center no-repeat;
}
</style>
<script>
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#birds" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
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;
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds" size="50">
</div>
</body>
</html>
php cdoe(它不完整,因为我不明白我需要在这个 php 页面中做什么。)
<?php
require_once("../frontend/config.php");
$term = $_GET['term'];
$sql = mysqli_query($link, "SELECT family_name FROM contact_details WHERE family_name LIKE '%$term%' ");
while($res = mysqli_fetch_array($sql)){
$family_name = $res['family_name'];
echo json_encode($family_name);
}
?>
有什么可以帮助我的吗?我怎样才能像链接一样显示搜索结果?注意:我正在学习 Jquery :) 谢谢。
【问题讨论】: