[英]Can't return value of dynamically created checkbox
I am creating form fields within my table, I then use javascript to create the rows in the table and build the rows with the needed fields. 我在表格中创建表单字段,然后使用javascript在表格中创建行并使用所需字段构建行。 below is my 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);
}
}
}
After I create my rows, I can return the values of the textboxes but not the check box elements. 创建行之后,我可以返回文本框的值,但不能返回复选框元素的值。 I am using document.getElementByID('deleteteam1').value
to return the value, but I get the following error. 我正在使用document.getElementByID('deleteteam1').value
返回值,但是出现以下错误。
Uncaught TypeError: Cannot read property 'value' of null 未捕获的TypeError:无法读取null的属性“值”
Keep in mind if I use document.getElementByID('agileteams1').value
it return the value. 请记住,如果我使用document.getElementByID('agileteams1').value
它将返回该值。
What I am doing wrong? 我做错了什么?
You have extra single quotes which is creating the wrong id property value for your checkbox element. 您有多余的单引号,这为复选框元素创建了错误的id属性值。 So when you access document.getElementByID('agileteams1')
, it is going to return null
as it does not exist. 因此,当您访问document.getElementByID('agileteams1')
,它将返回null
因为它不存在。 we cannot call any method/properties on a null
我们不能在null
上调用任何方法/属性
Change 更改
var deleteTeam="<input type='checkbox' value='1' id='deleteteam'"+x+"''>";
to 至
var deleteTeam="<input type='checkbox' value='1' id='deleteteam"+x+"'>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.