我正在使用ajax在mysql数据库上使用关键字搜索,当结果即将到来时我想用onclick函数显示选项为href,搜索工作正常,但当我点击任何选项时我得到一个我的firebug上的引用错误,这是代码: 的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 />";
}
}
?>
您可以在this url
看到工作页面你能告诉我怎么解决吗?或者我做错了什么?
感谢
答案 0 :(得分:0)
aca($valorCodificado);
将生成JavaScript,该JavaScript使用变量(可能不存在)调用函数。看起来你想要一个字符串。字符串需要引号。