2016-08-10 10 views
0

私は列からすべての値を取得する必要があります(もちろんヘッダー値は除く)。次に配列に列の値を設定する必要があります。私はネイティブJavaScriptに制限されています。columnからすべての値を取得し、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; 

IカラムNameの値を持つ配列を移入したいです。

+0

を必要としないので、記述的であると思いますか?またはusefによって変更されていますか? –

+0

@Jonaswはい、そうです。これらは動的に追加されます。ユーザーはフォームを塗りつぶしてボタンをクリックし、新しい行がテーブルに追加されます...作成テーブルの行は空です... –

答えて

1

あなたは行っています。私は、コードを使用すると、作成時にそれらを格納いけないなぜそれがさらに説明:)

var firstCells = document.querySelectorAll('td:nth-child(2)'); 
var cellValues = []; 
firstCells.forEach(function(singleCell) { 
    cellValues.push(singleCell.innerText); 
}); 
console.log(cellValues); 
+0

document.querySelectorAllはコードごとにすべてのテーブルのtdを探します。最善の方法は、より具体的にすることです。たとえば、次のようにします。var table = document.getElementById( 'table'); table.querySelectorAll( 'td:first-child'); –

+0

@KrzysztofZbicińskiこれを投稿していただきありがとうございます...それは最初の子供がいて、2番目が必要なので、働いています... :) –

+0

'td:nth-​​child(2)'を使用してください。私は答えを更新しました。 :) @ nemo_87 –

0
//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); 
}); 
0

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);

関連する問題