2015-12-08 42 views
0

我在表中创建表单字段,然后使用javascript在表中创建行并使用所需字段构建行。下面是我的JavaScript。无法返回动态创建的复选框的值

function createRow() { 
    for (var i=0;i<rows3;i++){ 

     if (document.getElementById('buID').value==agile1[i]['BU_ID']){ 
      var rowCount = document.getElementById("mytable").rows.length; 
      document.getElementById("rowIDcount").value=rowCount; 
      console.log(agile1[i]['Agile_team']); 
      x=x+1; 
    var row = document.createElement('tr'); // create row node 
    var rowid = document.createElement('td'); 
    var col = document.createElement('td'); // create column node 
    var col2 = document.createElement('td'); // create second column node 
    var col3 = document.createElement('td'); 
    var col4 = document.createElement('td'); 
    var col5 = document.createElement('td'); 
    var col6 = document.createElement('td'); 

    row.appendChild(rowid); 
    row.appendChild(col); // append first column to row 
    row.appendChild(col2); // append second column to row 
    row.appendChild(col3); 
    row.appendChild(col4); 
    row.appendChild(col5); 
    row.appendChild(col6); 


    var rowids="<input id='row"+x+"'readonly value='"+x+"'>"; 
    agile="<input id='agileteams"+x+"'readonly value='"+agile1[i]['Agile_team']+"'>"; 
    var strategic="<input type='number' min='0' max='100' id='strategic"+x+"'>"; 
    var Initiatives="<input type='number' min='0' id='numInts"+x+"'>"; 
    var NewName="<input id='newname"+x+"'>"; 
    var deleteTeam="<input type='checkbox' value='1' id='deleteteam'"+x+"''>"; 
    var RenameTeam="<input type='checkbox' id='renameteam'"+x+"''>"; 


    rowid.innerHTML=rowids; 
    col.innerHTML=agile; 
    col2.innerHTML=strategic; 
    col3.innerHTML=Initiatives; 
    col4.innerHTML=deleteTeam; 
    col5.innerHTML=RenameTeam; 
    col6.innerHTML=NewName; 
    var table = document.getElementById("mytable"); // find table to append to 
    table.appendChild(row); 

} 
} 
} 

后创建我行,我可以返回文本框的值,而不是复选框元素。我正在使用document.getElementByID('deleteteam1').value来返回值,但出现以下错误。

遗漏的类型错误:无法读取空

的特性“值”请记住,如果我用document.getElementByID('agileteams1').value它返回的值。

我做错了什么?

回答

3

您有多余的单引号会为您的复选框元素创建错误的id属性值。所以当你访问document.getElementByID('agileteams1')时,它将返回null,因为它不存在。我们不能在null

变化

var deleteTeam="<input type='checkbox' value='1' id='deleteteam'"+x+"''>"; 

调用任何方法/属性

var deleteTeam="<input type='checkbox' value='1' id='deleteteam"+x+"'>"; 
+0

哇,谢谢,我不能相信我没有注意到之前。总是有助于让别人注意它 –