-3

我正在使用 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看到工作页面

你能告诉我如何解决它吗?或者我做错了什么?

谢谢

4

1 回答 1

0

aca($valorCodificado);将生成 JavaScript 调用带有变量(可能不存在)作为参数的函数。看起来你想要一个字符串。字符串需要引号。

于 2012-11-03T22:38:53.823 回答