无法触发预输入功能
Unable to trigger typeahead function
我想通过关注 this 添加一个预输入功能。以下是我的编码方式。当我检查 control.log 时,我没有看到任何返回。似乎 typeahead 函数从未被调用,因为我测试了数据搜索 php 并且它确实有效。谁能指出我做错了什么?谢谢
<html>
<head>
<meta chatset="utf-8">
<title></title>
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="well">
<input type="text" class="span3" id="typeahead" data-provide="typeahead">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/typeahead.bundle.min.js"></script>
<script>
$(function() {
$("#typeahead").typeahead({
source: function(query, process) {
$.ajax({
url: 'source.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data) {
control.log(data);
}
});
}
});
});
</script>
</body>
</html>
<?php
if (isset($_POST['query'])) {
$db_host="localhost";
$db_username="myuser";
$db_pass="mypassword";
$db_name="mydb";
mysql_connect("$db_host", "$db_username", "$db_pass") or die(mysql_error()); mysql_select_db("$db_name")
or die ("Database not found");
$query=$_POST['query'];
$sql=mysql_query("SELECT prvname FROM prv WHERE prvname LIKE '%{$query}%'")
or die(mysql_error());
$array=array();
while ($row=mysql_fetch_assoc($sql)) {
$array[]=$row['prvname'];
}
echo json_encode($array);
}
?>
typeahead 函数需要两个参数,因此如果您不需要传递任何 typeahead 选项,您可以将 null 作为第一个参数传递:
$(function() {
$("#typeahead").typeahead(null, {
source: function(query, process) {
$.ajax({
url: 'source.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data) {
console.log(data);
}
});
}
});
});
我想通过关注 this 添加一个预输入功能。以下是我的编码方式。当我检查 control.log 时,我没有看到任何返回。似乎 typeahead 函数从未被调用,因为我测试了数据搜索 php 并且它确实有效。谁能指出我做错了什么?谢谢
<html>
<head>
<meta chatset="utf-8">
<title></title>
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="well">
<input type="text" class="span3" id="typeahead" data-provide="typeahead">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/typeahead.bundle.min.js"></script>
<script>
$(function() {
$("#typeahead").typeahead({
source: function(query, process) {
$.ajax({
url: 'source.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data) {
control.log(data);
}
});
}
});
});
</script>
</body>
</html>
<?php
if (isset($_POST['query'])) {
$db_host="localhost";
$db_username="myuser";
$db_pass="mypassword";
$db_name="mydb";
mysql_connect("$db_host", "$db_username", "$db_pass") or die(mysql_error()); mysql_select_db("$db_name")
or die ("Database not found");
$query=$_POST['query'];
$sql=mysql_query("SELECT prvname FROM prv WHERE prvname LIKE '%{$query}%'")
or die(mysql_error());
$array=array();
while ($row=mysql_fetch_assoc($sql)) {
$array[]=$row['prvname'];
}
echo json_encode($array);
}
?>
typeahead 函数需要两个参数,因此如果您不需要传递任何 typeahead 选项,您可以将 null 作为第一个参数传递:
$(function() {
$("#typeahead").typeahead(null, {
source: function(query, process) {
$.ajax({
url: 'source.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data) {
console.log(data);
}
});
}
});
});