2015-11-11 78 views
0

我一直在我的代码中收到此错误。错误 - 未捕获TypeError:无法设置属性'innerHTML'的未定义

Uncaught TypeError: Cannot set property 'innerHTML' of undefined我已阅读了一些类似的错误Uncaught TypeError: Cannot set property 'innerHTML' of undefined of null和我已经尝试了一些建议的方法,但似乎没有任何改变。

它似乎指向这条线document.getElementsByClassName("demo")[i].innerHTML = xhttp.responseText;我不明白为什么像我之前添加循环,它工作正常。

我的代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 
<!--#include file="../Connections/DVerto.asp" --> 
<% 
Dim Recordset1 
Dim Recordset1_cmd 
Dim Recordset1_numRows 

Set Recordset1_cmd = Server.CreateObject ("ADODB.Command") 
Recordset1_cmd.ActiveConnection = MM_DVerto_STRING 
Recordset1_cmd.CommandText = "SELECT Part_Number FROM dbo.Stock_Header WHERE Part_Number like '84%'" 
Recordset1_cmd.Prepared = true 

Set Recordset1 = Recordset1_cmd.Execute 
Recordset1_numRows = 0 
%> 
<% 
Dim Repeat1__numRows 
Dim Repeat1__index 

Repeat1__numRows = 10 
Repeat1__index = 0 
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows 
%> 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body onLoad="loadDoc()"> 
<table width="50%" border="0" cellspacing="2" cellpadding="2"> 
    <% 
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF)) 
%> 
    <tr> 
    <td class="prodref"><%=(Recordset1.Fields.Item("Part_Number").Value)%></td> 
    <td class="demo">&nbsp;</td> 
    </tr> 
    <% 
    Repeat1__index=Repeat1__index+1 
    Repeat1__numRows=Repeat1__numRows-1 
    Recordset1.MoveNext() 
Wend 
%> 
</table> 
<script> 
var a = document.getElementsByClassName("prodref").length; 
var i = 0; 
for (i; i < a; i++) { 
    loadDoc(); 
} 
function loadDoc() { 
    var a = document.getElementsByClassName("prodref"); 
    a[i] = document.getElementsByClassName("prodref").innerHTML; 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     document.getElementsByClassName("demo")[i].innerHTML = xhttp.responseText; 
    } 
    }; 
    xhttp.open("GET", "data.asp?prodref="+a[i].innerHTML, true); 
    xhttp.send(); 
} 
</script> 

</body> 
</html> 
<% 
Recordset1.Close() 
Set Recordset1 = Nothing 
%> 
+0

你有没有叫“demo”的类? –

+0

@LJa是的,我的一个表格单元格有一类'demo' - '' –

回答

1

你需要传递i的功能,因为在循环完成由AJAX请求返回的时间。

var prodref = document.getElementsByClassName("prodref"); 
var demo = document.getElementsByClassName("demo"); 
var i = 0; 
var a = prodref.length; 

for (i; i < a; i++) { 
    loadDoc(i); 
} 

function loadDoc(i) { 
    console.log("creating loadDoc() call for", i); 

    var xhttp = new XMLHttpRequest(); 

    xhttp.onreadystatechange = function() { 
    console.log("callback invoked for", i, "with state:", xhttp.readyState); 

    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     console.log("setting .innerHTML for", i, "to:", xhttp.responseText); 
     demo[i].innerHTML = xhttp.responseText; 
    } 
    }; 

    xhttp.open("GET", "data.asp?prodref="+prodref[i].innerHTML, true); 
    xhttp.send(); 
} 

现在,有一种i局部变量的loadDoc每个单独调用和回调函数,你在函数创建将关闭在当地i

这是一个关闭

我也缓存了你的DOM选择。除非你认为它们会改变,否则你不需要一遍又一遍地重复读取节点。

+0

感谢您的支持。它可以工作,但它的表现有点不稳定。有时它会返回4行中的2个结果,然后是4或3中的4个。此外,它有时会错过一行?有什么建议么? –

+0

@JonathanGriffin:你需要在代码的不同位置添加一些'console.log()'调用来进行一些调试,以查看发生了什么以及何时发生。例如,查看是否为每个项目调用了回调,然后查看是否调用了“if”条件。我会给答案增加一些例子。 –

+0

另外,如果可能的话,只发送一个请求并告诉它需要多少结果会更好。 JSON是发送数据集合的常用格式。 –

1

你正在做一个异步请求,以便变量不会是你所期望的,当的onreadystatechange函数被调用。

试试这个:

... 
for (i; i < a; i++) { 
    loadDoc(i); 
} 

function loadDoc(i) { 
... 
相关问题