0

我得到了这个 javascript 代码,我试图通过使用带有 onclick=function 的按钮类型从我的 html 调用。不幸的是,它给了我错误:函数“loadData()”未定义,我不明白为什么。这是我的 JavaScript 代码:

function loadData(url) {
    var serialNumber = document.getElementById('searchBox').value;
    var xmlhttp;
    var txt, xx, x, i;
    alert(serialNumber);
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            txt = "<table border='1'><tr><th>Serial</th><th>Product number</th><th>Product name</th><th>Account name</th><th>Document number</th></tr>";
            x = xmlhttp.responseXML.documentElement.getElementsByTagName("PRODUCT");
            for (i = 0; i < x.length; i++) {
                if (
                txt = txt + "<tr>";
                xx = x[i].getElementsByTagName("SERIAL");
                {
                    try {
                        txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td>&nbsp;</td>";
                    }
                }
                xx = x[i].getElementsByTagName("PRODNM");
                {
                    try {
                        txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td>&nbsp;</td>";
                    }
                }
                xx = x[i].getElementsByTagName("PRODNR");
                {
                    try {
                        txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td>&nbsp;</td>";
                    }
                }
                xx = x[i].getElementsByTagName("ACCNAME");
                {
                    try {
                        txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td>&nbsp;</td>";
                    }
                }
                xx = x[i].getElementsByTagName("NRDOC");
                {
                    try {
                        txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                    }
                    catch (er) {
                        txt = txt + "<td>&nbsp;</td>";
                    }
                }
                txt = txt + "</tr>";
            }
            txt = txt + "</table>";
            document.getElementById('showTable').innerHTML = txt;
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

这是调用该函数的 HTML 代码:

<input type="text" id="searchBox" value="Enter serial number here" onFocus="this.value=''"/>
<input type="button" id="search" value="Search" onclick="loadData('prod_catalog.xml')"/>
4

2 回答 2

0

问题是贝特拉巴所说的“如果”。问题不在于它没有关闭,问题在于我试图做一些失败的事情,不知何故我忘记了那一点。感谢您的及时回复!:)

于 2014-01-27T14:48:45.913 回答
0

这是因为您没有正确包含函数代码,或者函数 loadData 之前的代码中有错误。检查控制台是否有错误。还要检查是否定义了 loadData 。

于 2014-01-27T14:30:24.060 回答