我有一个 html 表单、php 脚本和 jquery。我需要一个 ajax 代码来从我的 php 脚本中进行自动建议。以下是代码...
表单.html
<html>
<head>
<script src="jquery1.6.4.min.js" type="text/javascript"></script>
<script src="jquery.jSuggest.js" type="text/javascript"></script>
<link href="jSuggest.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" name="form1" method="post" action="#">
<input type="text" name="TagsInputField" id="TagsInputField"/>
</form>
</body>
</html>
测试.php
<?php
include("bc/script/core/dbcon.php");
$input = $_POST['TagsInputField'];
$data = array();
// query your DataBase here looking for a match to $input
$query = mysql_query("SELECT * FROM user WHERE username LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['username'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>
jquery.jSuggest.js
$(function() {
var dataSource = {
items: [
{
value: "21",
name: "Mick Jagger"},
{
value: "43",
name: "Johnny Storm"},
{
value: "46",
name: "Richard Hatch"},
{
value: "54",
name: "Kelly Slater"},
{
value: "79",
name: "Michael Jordan"}
]
};
$('#TagsInputField').jSuggest({
source: dataSource.items,
selectedItemProp: "name",
seekVal: "name",
selectionAdded: function(elem, data) {
console.log(data.name);
},
selectionRemoved: function(elem, data) {
console.log(data.name);
elem.remove();
}
});
});
注意指针“源”它引用对象“dataSource.items”来读取建议。任何人都可以帮助我编写一个 ajax 代码来读取返回 json 的 php 文件的建议。