我正在使用 ajax 在 mysql 数据库上使用关键字搜索,当结果出现时,我想将选项显示为带有 onclick 功能的 href,搜索工作正常,但是当我单击任何选项时,我得到一个参考错误我的萤火虫,这是代码:index.php
<html>
<body>
<style>
#displayDiv {
background-color: #ffeeaa;
width: 200;
}
</style>
<script type="text/javascript">
function aca(esto) {
var esta = esto;
alert(esta);
}
</script>
<script type="text/javascript">
function ajaxFunction(str) {
var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
httpxml = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateChanged() {
if (httpxml.readyState == 4) {
document.getElementById("displayDiv").innerHTML = httpxml.responseText;
}
}
var url = "search.php";
url = url + "?txt=" + str;
url = url + "&sid=" + Math.random();
httpxml.onreadystatechange = stateChanged;
httpxml.open("GET", url, true);
httpxml.send(null);
}
</script>
</head>
<body>
<form name="myForm">
<input type="text" onkeyup="ajaxFunction(this.value);" name="username"
/>
<div id="displayDiv"></div>
</form>
</body>
这是search.php
<?php
include ("dbinfo.php");
$in = $_GET['txt'];
$msg = "";
if (strlen($in) > 0 and strlen($in) < 20) {
$t = mysql_query("select RubroId, RubroDisp from rubros where KeyWords like '$in%'");
while ($nt = mysql_fetch_array($t)) {
$valor = $nt[RubroDisp];
$valorCodificado = str_replace(" ", "_", $valor);
echo "<a href='#' onclick='aca($valorCodificado);'>$valor</a><br />";
}
}
?>
您可以在此 url看到工作页面
你能告诉我如何解决它吗?或者我做错了什么?
谢谢