我正在尝试自动提供搜索框,但我找不到任何方法,但是当我点击列表中的某个项目以使文本进入文本框时,我无法找到方法。一旦发生这种情况,我还需要列表消失。有人知道怎么做吗?请找我附上的代码:
的index.php
<html>
<head>
<script type="text/javascript">
function findmatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'search.inc.php?search_text='+document.search.search_text.value, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="search" name="search">
Type a name:<br />
<input type="text" id = "search_text" name="search_text" onkeyup="findmatch();">
</form>
<ul>
<div id="results"></div>
</ul>
</body>
</html>
Search.inc.php
<?php
if (isset($_GET['search_text'])) {
$search_text = $_GET['search_text'];
}
if (!empty($search_text)){
if (@mysql_connect('localhost','username','password')) {
if (@mysql_select_db('Database')){
$query = "SELECT name FROM Customers WHERE name LIKE '$search_text%'";
$query_run = mysql_query($query);
while ($query_row = mysql_fetch_assoc($query_run)){
$id = "id".$i;
echo "<li>".$name = $query_row['name']." </li>";
}
}
}
}
?>
答案 0 :(得分:1)
您不是从任何地方调用Clicked()函数。尝试将事件添加到要输出的列表项中:
echo "<li onclick='Clicked(this)'>".$name = $query_row['name']." </li>";
而且您还想知道点击功能中点击的内容“
function Clicked(li)
{
document.getElementById("aaa").value = li.innerHTML;
document.getElementById("results").innerHTML = '';
return false; // so no postback
}