Php ') { msg='请求的JSON解析失败'; }else if(异常==='timeout'){ msg='超时错误'; }否则如果(异常==='abort'){ msg='Ajax请求已中止'; }否则{ msg='未捕获错误。\n'+jqXHR.re
') { msg='请求的JSON解析失败'; }else if(异常==='timeout'){ msg='超时错误'; }否则如果(异常==='abort'){ msg='Ajax请求已中止'; }否则{ msg='未捕获错误。\n'+jqXHR.responseText; } 警报(msg); }, }); }, 最小长度:1, 选择:功能(事件、用户界面){ $(“#自动完成”).val(ui.item?ui.item.name:); showMember(ui.item.name); 返回false; }, 更改:功能(事件、用户界面){ 如果(!ui.item){ $(“#自动完成”).val(“”); $(“#txtHint”).val(“”); } } }).autocomplete(“实例”)。\u renderItem=函数(ul,项){ 返回$(“Php ') { msg='请求的JSON解析失败'; }else if(异常==='timeout'){ msg='超时错误'; }否则如果(异常==='abort'){ msg='Ajax请求已中止'; }否则{ msg='未捕获错误。\n'+jqXHR.re,php,jquery,html,Php,Jquery,Html,') { msg='请求的JSON解析失败'; }else if(异常==='timeout'){ msg='超时错误'; }否则如果(异常==='abort'){ msg='Ajax请求已中止'; }否则{ msg='未捕获错误。\n'+jqXHR.responseText; } 警报(msg); }, }); }, 最小长度:1, 选择:功能(事件、用户界面){ $(“#自动完成”).val(ui.item?ui.item.name:); showMember(ui.item.name);
<script type="text/javascript">
$(function() {
$( "#autocomplete" ).autocomplete({
source: 'server.php'
});
});
</script>
</head>
<body>
<div id="wrapper">
<div class="ui-widget">
<input type="text" id="autocomplete" name="search" onclick="showMember(this.value)">
</div>
</div>
<div id="txtHint"></div>
<script>
function showMember(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","catlist.php?name="+str,true);
xmlhttp.send();
}
}
</script>
$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);
$searchTerm = $_GET['term'];
$select =mysql_query("SELECT * FROM cmsb_markers WHERE name LIKE '".$searchTerm."%'");
while ($row=mysql_fetch_array($select))
{
$data[] = $row['name'];
}
echo json_encode($data);
<input type="text" id="autocomplete" name="search">
$(function() {
$( "#autocomplete" ).autocomplete({
source: 'server.php',
onSelect: showMember(e, term, item)
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Tests</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<script>
$(function(){
$("#autocomplete").autocomplete({
search: function( event, ui ) {
$('.loader').show();
},
source: function (request, response) {
$.ajax({
method: "get",
url: "server.php",
dataType: "json",
data: {
q: request.term
},
success: function (data) {
$('.loader').hide();
response(data);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
},
});
},
minLength: 1,
select: function (event, ui) {
$("#autocomplete").val(ui.item ? ui.item.name : "");
showMember(ui.item.name);
return false;
},
change: function (event, ui) {
if (!ui.item) {
$("#autocomplete").val("");
$("#txtHint").val("");
}
}
}).autocomplete("instance")._renderItem = function (ul, item) {
return $("<li>").append("<a>" + item.name + "</a>").appendTo(ul);
};
function showMember(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","catlist.php?name="+str,true);
xmlhttp.send();
}
}
});
</script>
</head>
<body>
<div id="wrapper">
<div class="ui-widget">
<input type="text" id="autocomplete" name="search" />
</div>
</div>
<div id="txtHint"></div>
</body>
</html>
<?php
//server.php
$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);
$searchTerm = $_GET['q'];
$select =mysql_query("SELECT * FROM cmsb_markers WHERE name LIKE '".$searchTerm."%'");
while ($row=mysql_fetch_array($select)) {
$data[] = array("name" => $row['name']);
}
echo json_encode($data);