[英]Cannot get value from input text of Dynamically created Table
Is that possible to get values from dynamically created table after a button click? 单击按钮后是否可以从动态创建的表中获取值?
Here is my code for creating a table with an input field: 这是我的用于创建带有输入字段的表的代码:
function tableInsert(row,col){
var body = document.body,
tbl = document.createElement('table');
tbl.style.width = '100px';
tbl.style.border = '1px solid black';
tbl.id="draw_table";
for(var i = 0; i < col; i++){
var tr = tbl.insertRow();
for(var j = 0; j < row; j++){
var input1 = document.createElement("input");
var td = tr.insertCell();
td.appendChild(input1);
td.style.border = '1px solid black';
}
}
body.appendChild(tbl);
console.log(tbl.id);
}
Here is the button click function. 这是按钮点击功能。 It gets the value of the table which is dynamically created: 它获取动态创建的表的值:
$("#calculate").click(function(){
var table = document.getElementById('draw_table');
for (var r = 0, n = table.rows.length; r < n; r++) {
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
console.log(table.rows[r].cells[c].value);//value is undefined . is it right way to get the value
}
}
});
your code retrieve td element while you need value of input tag! 当您需要输入标签的值时,您的代码将检索td元素!
change your code to this: 将您的代码更改为此:
$("#calculate").click(function(){
var table = document.getElementById('draw_table');
for (var r = 0, n = table.rows.length; r < n; r++) {
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
console.log(table.rows[r].cells[c].children[0].value);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.