2015-12-08 44 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

哇,謝謝,我不能相信我沒有注意到之前。總是有助於讓別人注意它 –