我得到了这个 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> </td>";
}
}
xx = x[i].getElementsByTagName("PRODNM");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("PRODNR");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("ACCNAME");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("NRDOC");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </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')"/>