明白了:你沒有得到這裏的文本框的值var search_term = $(this).val();
的OP試圖讀取使用var search_term = $(this).attr('value');
有你的選擇查詢的問題,我已經更新它的文本框的值。
更新1:
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$conn=mysql_connect("localhost","root","") or die ("could not connect");
mysql_select_db("test") or die ("could not find db");
if (!empty($_POST['search_term']))
{
$search_term = $_POST['search_term'] ;
$query = mysql_query("SELECT `ingName` FROM `ing` WHERE `ingName` LIKE '".$search_term.'%', $conn);
if($query)
{
while($row = mysql_fetch_assoc($query))
{
echo '<li>'.$row['ingName'].'</li>';
}
}
}
mysql_close($conn);
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dashboard</title>
</head>
<body>
<div class="container">
<input type="text" name='search_term' class="searchFunction">
<input type="submit" value="Search">
<div class="dropdown">
<ul class="result"></ul>
</div>
</div>
<script src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.searchFunction').keyup(function(event) {
var search_term = $(this).val();
$.post(document.location.href, { search_term:search_term }, function(data) {
$('.result').html(data);
$('.result li').click(function(event) {
var result_value = $(this).text();
$('.searchFunction').attr('value', result_value);
$('.result').html('');
});
});
});
});
</script>
</body>
有什麼錯誤? – AliN11
什麼也沒有,它的奇怪的 – Nevershow2016
@ Nevershow2016你的代碼正在運行。檢查你的控制檯。我只是驗證它。你的查詢有問題。 –