简体   繁体   English

使用jquery循环遍历表列以推送到Google App脚本

[英]Looping through column of table with jquery to push to Google App Script

I'm really stuck at creating a loop to get the 1st column data of a table with jquery. 我真的很想创建一个循环以使用jquery获取表的第一列数据。 The table has the following structure: 该表具有以下结构:

<table id="table">
<tbody id="tbody">
  <tr id="tr1">
   <td id="td1_1"><input id="td1_1_number" type="number" /></td>
   <td id="td1_2"><input id="td1_2_number" type="number" /></td>
   <td id="td1_3"><input id="td1_3_number" type="number" /></td>
   <td id="td1_4"><input id="td1_4_number" type="number" /></td>
  </tr>
  <tr id="tr2">
   <td id="td2_1"><input id="td2_1_number" type="number" /></td>
   <td id="td2_2"><input id="td2_2_number" type="number" /></td>
   <td id="td2_3"><input id="td2_3_number" type="number" /></td>
   <td id="td2_4"><input id="td2_4_number" type="number" /></td>
  </tr>
 </tbody>
</table> 

At the moment I'm reading each of them individually but there are a lot of td elements in the table. 目前,我正在逐个阅读它们,但是表中有很多td元素。 The code I'm using atm is the following: 我正在使用atm的代码如下:

var data ={
td1_1_number:  $("#td1_1_number").val(),
td1_2_number:  $("#td1_2_number").val(),
td1_3_number:  $("#td1_3_number").val(),
td1_4_number:  $("#td1_4_number").val(),
td2_1_number:  $("#td2_1_number").val(),
td2_2_number:  $("#td2_2_number").val(),
td2_3_number:  $("#td2_3_number").val(),
td2_4_number:  $("#td2_4_number").val()    
}

I tried to use each method but got stuck because I need this array structure to be intact for pushing the data with GAS. 我尝试使用每种方法,但都卡住了,因为我需要完整的数组结构来通过GAS推送数据。

The data I'm appending in gs: 我要在gs中附加的数据:

 sheet.appendRow([data.td1_1_number, data.td1_2_number, data.td1_3_number, data.td1_4_number, data.td2_1_number, data.td2_2_number, data.td2_3_number, data.td2_4_number])

I really would like to change to a loop approach since mine is not very elegant. 我真的很想更改为循环方法,因为我的方法不是很优雅。

Here is an example of using the reduce method to create the object from all the inputs. 这是一个使用reduce方法从所有输入创建对象的示例。

  //get an array of all the input elements and reduce them to a single object var result = $('#tbody td input').get().reduce(function(data, checkbox){ data[ checkbox.id ] = checkbox.value; //return our modified data reduction return data; }, {}); //show the result console.log(result); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table"> <tbody id="tbody"> <tr id="tr1"> <td id="td1_1"><input id="td1_1_number" type="number" value="1" /></td> <td id="td1_2"><input id="td1_2_number" type="number" value="2" /></td> <td id="td1_3"><input id="td1_3_number" type="number" value="3" /></td> <td id="td1_4"><input id="td1_4_number" type="number" value="4" /></td> </tr> <tr id="tr2"> <td id="td2_1"><input id="td2_1_number" type="number" value="99" /></td> <td id="td2_2"><input id="td2_2_number" type="number" value="98" /></td> <td id="td2_3"><input id="td2_3_number" type="number" value="97" /></td> <td id="td2_4"><input id="td2_4_number" type="number" value="96" /></td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM