当我在文本框中输入任何内容时,它不会显示任何内容。
如果我硬编码$searchTerm='a';
可能这是我缺少的jQuery中的东西。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#skills" ).autocomplete({
source: 'search.php'
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="skills">skills: </label>
<input id="skills">
</div>
</body>
</html>
我的PHP(search.php)页面是:
//get search term
$searchTerm = $_GET['term'];
//get matched data from skills table
$query = $db->query("SELECT * FROM skills WHERE skill LIKE '%".$searchTerm."%' ORDER BY skill ASC");
while ($row = $query->fetch_assoc()) {
$data[] = $row['skill'];
}
//return json data
echo json_encode($data);
Json
["A A EDU","A T Still","A.B.I. ","Aani", "Zane"]
Chrome调试
答案 0 :(得分:2)
我无法看到发送params(数据)给您的服务器调用。
像这样(但如果这项工作,我还没对):$(function() {
$( "#skills" ).autocomplete({
source: 'search.php',
data: {
'term' : $('#skills').val()
},
type: "GET"
});
});
我更喜欢使用插件来执行此操作,在AJAX方法中可以将结果设置为在字段中列出。像这样:
$('#field_auto_complete').autocomplete({
lookup: function (query, done) {
$.ajax({
url: "form.php",
dataType: "json",
data: {
term : query
},
type: "GET", //you can change to POST if u need
success: function( response ){
var result = {
suggestions: response.results
};
done(result);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$.redirect('../error');
}
});
},
onSelect: function (suggestion) {
id_ofselected = suggestion.data; //set a var with selected object
}
});
在Php Side你有这样的代码:
$digits = $_GET['term'];
$query = $db->query(SELECT name AS `value`, id AS `data`
FROM table
WHERE name LIKE '$digits%'");
while ($row = $query->fetch_assoc()) {
$data[] = $row['skill'];
}
if(isset($data[0])){
echo json_encode(array( 'results' => $cons ));
}else{
echo json_encode(array( 'results' => array());
}
我使用Autocomplete Jquery插件。见[链接](https://github.com/devbridge/jQuery-Autocomplete)!
答案 1 :(得分:-2)
{
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
}