从列中获取所有值,并将它们放入javascript中的数组中
Get all values from column and put them into an array in javascript
我需要从列中获取所有值(当然除了标题值),然后用列值填充一个数组。我只能使用原生JavaScript。
这是一个表头:
<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;
再次,我想用Name
列中的值填充一个数组。
好了。我认为代码是描述性的,不需要进一步的解释:)
var firstCells = document.querySelectorAll('td:nth-child(2)');
var cellValues = [];
firstCells.forEach(function(singleCell) {
cellValues.push(singleCell.innerText);
});
console.log(cellValues);
//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 table = document.getElementById("result"); //ur table id
var rows = table.getElementsByTagName("tr");
var output = [];
for(var i=0;i<rows.length;i++)
{
output.push(rows[i].getElementsByTagName("td")[1].innerHTML);//instead of 1 pass column index
}
alert(output);
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 将Javascript数组发送到控制器ASP.NET MVC
- JavaScript数组包含一个值
- 将JavaScript数组传递给函数
- javascript:数组循环
- 将数据存储在javascript数组中以供进一步使用
- 在Javascript数组中查找绝对最大值
- JavaScript数组优化以提高性能
- 在javascript数组中分散数字
- 如何发送分配列表<字符串>到JavaScript数组或可枚举对象
- Javascript数组动态
- javascript数组元素是否知道其封闭数组
- 将 JSON 数组解析为 JavaScript 数组
- 将Transform和Instance Variable转换为对象的JavaScript数组
- 使用Web Html表单创建Javascript数组
- 从重复的javascript数组结果集中只获取一行
- 如何为Javascript数组()的使用准备PHP变量
- Javascript数组和函数
- 为DataTables aoColumnDefs创建JavaScript数组(JSON格式)