从列中获取所有值,并将它们放入javascript中的数组中
我需要从一列中获取所有值(当然除了标题值),然后用列值填充一个数组。我仅限于使用本机JavaScript 这是一个表格标题:从列中获取所有值,并将它们放入javascript中的数组中,javascript,Javascript,我需要从一列中获取所有值(当然除了标题值),然后用列值填充一个数组。我仅限于使用本机JavaScript 这是一个表格标题: <table id="results" width="360" border="1"> <thead> <tr> <th scope="col" width="120">Date Created</th> <th scope="col" width="12
<table id="results" width="360" border="1">
<thead>
<tr>
<th scope="col" width="120">Date Created</th>
<th scope="col" width="120">Name</th>
<th scope="col" width="120">Tests</th>
</tr>
</thead>
</table>
创建日期
名称
测验
这就是创建行的方式:
var table = document.getElementById("results");
var name = document.getElementById("tbName").value;
var elements = document.getElementsByTagName("select")
var testArray = [];
var test;
for(var i=0; i < elements.length ; i++)
{
testArray[i] = elements[i].value;
}
test = testArray.join(',');
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
//In the first step using InsertRow function you are creating a first row i.e tr
var row = table.insertRow(table.rows.length);
//In the second step you create a cell i.e td dynamically
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
// Here in step 3 you can add data to your cells from the modal popup inputs(According to your logic)
cell1.innerHTML = today;
cell2.innerHTML = name;
cell3.innerHTML = test;
var table=document.getElementById(“结果”);
var name=document.getElementById(“tbName”).value;
var elements=document.getElementsByTagName(“选择”)
var testArray=[];
var检验;
对于(var i=0;i 如果(dd就这样了。我认为代码非常具有描述性,不需要进一步解释:)
var table=document.getElementById(“结果”)//ur表id
var rows=table.getElementsByTagName(“tr”);
var输出=[];
对于(var i=0;i为什么不在创建时存储它们?或者它们是由usef更改的?@Jonasw是的。它们是动态添加的。用户填写表单,单击按钮,新行被添加到表中…因此在创建表时,行是空的…document.queryselectoral将根据您的代码在所有表中查找td。最好的方法是使其更具体fic.for eg:var table=document.getElementById('table');table.queryselectoral('td:first child');@KrzysztofZbiciński感谢您发布这篇文章……它在某种程度上起作用,因为它需要第一个孩子,我需要第二个孩子……:)然后使用td:nth child(2)
。我已经更新了答案。:@nemo W
//get parent element
parent=document.getElementById("name");
//get all tags inside this element
childs=parent.getElementsByTagName("td or sth like that");
//create an empty array
names=[];
//loop through childs
childs.forEach(child){
//add child balue to array
names.push(child.value);
});
var firstCells = document.querySelectorAll('td:nth-child(2)');
var cellValues = [];
firstCells.forEach(function(singleCell) {
cellValues.push(singleCell.innerText);
});
console.log(cellValues);