从动态HTML表中获取数据的最简单方法

时间:2022-12-04 15:52:05

I am trying to get data from a dynamic HTML table. The user has to option to add or delete rows (through a javascript script) and then enter data into the table input fields. I need a way to get the data out of the table. I realize that javascript can not write to a file, but are there any other options?

我试图从动态HTML表中获取数据。用户必须选择添加或删除行(通过javascript脚本),然后将数据输入到表输入字段中。我需要一种方法来从表中获取数据。我意识到javascript无法写入文件,但还有其他选项吗?

Thank you kindly. All help is greatly greatly appreciated!

非常感谢你。非常感谢所有帮助!

**Edit: I am adding and deleting rows/columns to the table, so the text inputs are dynamically created and I am trying to get the values from those fields.

**编辑:我正在向表中添加和删除行/列,因此文本输入是动态创建的,我试图从这些字段中获取值。

1 个解决方案

#1


0  

You could create a simple JS function to fish out data in an array form. I'm assuming a very simple table for this. I've used JQuery for ease but you can do the same with javascript..

您可以创建一个简单的JS函数来捕获数组形式的数据。我假设一个非常简单的表。我已经使用了JQuery轻松,但你可以用javascript做同样的事情。

<table id="mytable">
<tr>
  <td>1</td>
  <td>3</td>
  <td>4</td>
  <td>2</td>
</tr><tr>
  <td>5</td>
  <td>6</td>
  <td>7</td>
  <td>8</td>
</tr><tr>
  <td>9</td>
  <td>31</td>
  <td>42</td>
  <td>23</td>
</tr>
</table>
<script>
    getAllDataFromTable();
</script>

I've used JQuery for ease but you can do the same with javascript..

我已经使用了JQuery轻松,但你可以用javascript做同样的事情。

function getAllDataFromTable(){
    var mydata = Array();
  for(i=0;i<$('#mytable').find("tr").length;i++){
    mydata[i] = Array();
    var currentRow = $('#mytable').find("tr").eq(i);
    for(j=0; j< currentRow.find('td').length ;j++){
        mydata[i][j] = currentRow.find('td').eq(j).html()
    };
  };
  alert(JSON.stringify(mydata))
}

https://jsfiddle.net/9yuc3bvj/

#1


0  

You could create a simple JS function to fish out data in an array form. I'm assuming a very simple table for this. I've used JQuery for ease but you can do the same with javascript..

您可以创建一个简单的JS函数来捕获数组形式的数据。我假设一个非常简单的表。我已经使用了JQuery轻松,但你可以用javascript做同样的事情。

<table id="mytable">
<tr>
  <td>1</td>
  <td>3</td>
  <td>4</td>
  <td>2</td>
</tr><tr>
  <td>5</td>
  <td>6</td>
  <td>7</td>
  <td>8</td>
</tr><tr>
  <td>9</td>
  <td>31</td>
  <td>42</td>
  <td>23</td>
</tr>
</table>
<script>
    getAllDataFromTable();
</script>

I've used JQuery for ease but you can do the same with javascript..

我已经使用了JQuery轻松,但你可以用javascript做同样的事情。

function getAllDataFromTable(){
    var mydata = Array();
  for(i=0;i<$('#mytable').find("tr").length;i++){
    mydata[i] = Array();
    var currentRow = $('#mytable').find("tr").eq(i);
    for(j=0; j< currentRow.find('td').length ;j++){
        mydata[i][j] = currentRow.find('td').eq(j).html()
    };
  };
  alert(JSON.stringify(mydata))
}

https://jsfiddle.net/9yuc3bvj/