每当我的ajax代码打印数据时,在第一个col中它会写undefined
。我找不到任何解决方案。以下是它的打印方式:
<tbody id="impDisplay">
undefined
<tr>
<td>1498</td>
<td>Wordpress įdiegimas</td>
<td><input name="impTaskCh" id="1498" type="checkbox"></td>
</tr>
</tbody>
这是我的脚本打印所有内容:
function stateChanged(){
if(httpxml.readyState==4){
var data = JSON.parse(httpxml.responseText); // Received the data
var str;
for(var key in data.data){
str = str + "<tr><td>"+ key + "</td>"
str = str + "<td>"+ data.data[key] + "</td>"
str = str + "<td><input type='checkbox' name='impTaskCh' id='"+ key +"'></td></tr>"
}
//str = str + "</tr>";
document.getElementById("impDisplay").innerHTML=str;
}
}
请帮助duno如何解决这个问题?
答案 0 :(得分:3)
您将str
定义为未定义,因此当您附加到它时,您将附加到undefined上。将str
设置为初始空字符串:
var str; // Here, you've set str to undefined
str = str + // Now you're appending onto 'undefined'
// Appending onto 'undefined' will call toString() on it, converting it
// to a string and resulting in this...
str = "undefined<tr><td>...."
更改为:
var str = "";