简体   繁体   English

如何遍历表格行并使用 jQuery 获取单元格值

[英]How to iterate through a table rows and get the cell values using jQuery

I am creating the below table dynamically using jQuery... After executing my code I get the table as below:我正在使用 jQuery 动态创建下表...执行我的代码后,我得到如下表:

<table id="TableView" width="800" style="margin-left: 60px">
<tbody>
 <tr>
 <th>Module</th>
 <th>Message</th>
</tr>
<tr class="item">
 <td> car</td>
 <td>
  <input class="name" type="text">
 </td>
 <td>
<input class="id" type="hidden" value="5">
</td>
   </tr>
<tr class="item">
 <td> bus</td>
 <td>
  <input class="name" type="text">
 </td>
 <td>
<input class="id" type="hidden" value="9">
</td>
  </tr>

I used to iterate the table like this:我曾经像这样迭代表:

 $("tr.item").each(function() {
            var quantity1 = $this.find("input.name").val();
        var quantity2 = $this.find("input.id").val();

            });

By using the above query I am getting values of first row cells only... help me with jQuery that will iterate through the whole rows of the table and get the row cell values in quantity1 and quantity2 .通过使用上面的查询,我只获取第一行单元格的值...帮助我使用 jQuery,它将遍历表的整个行并获取quantity1quantity2的行单元格值。

$(this) instead of $this $(this)而不是 $this

$("tr.item").each(function() {
        var quantity1 = $(this).find("input.name").val(),
            quantity2 = $(this).find("input.id").val();
});

Proof_1:证明_1:

proof_2:证明_2:

Looping through a table for each row and reading the 1st column value works by using JQuery and DOM logic.使用 JQuery 和 DOM 逻辑为每一行循环一个表并读取第一列值。

var i = 0;
var t = document.getElementById('flex1');

$("#flex1 tr").each(function() {
    var val1 = $(t.rows[i].cells[0]).text();
    alert(val1) ;
    i++;
});

Hello every one thanks for the help below is the working code for my question大家好,感谢下面的帮助是我的问题的工作代码

$("#TableView tr.item").each(function() { 
    var quantity1=$(this).find("input.name").val(); 
    var quantity2=$(this).find("input.id").val(); 
});

You got your answer, but why iterate over the tr when you can go straight for the inputs?你得到了答案,但是当你可以直接获取输入时,为什么还要迭代 tr 呢? That way you can store them easier into an array and it reduce the number of CSS queries.这样你就可以更容易地将它们存储到数组中,并减少 CSS 查询的数量。 Depends what you want to do of course, but for collecting data it is a more flexible approach.当然取决于你想做什么,但对于收集数据来说,这是一种更灵活的方法。

http://jsfiddle.net/zqpgq/ http://jsfiddle.net/zqpgq/

var array = [];

$("tr.item input").each(function() {
    array.push({
        name: $(this).attr('class'),
        value: $(this).val()
    });
});

console.log(array);​

I got it and explained in below:我得到它并在下面解释:

//This table with two rows containing each row, one select in first td, and one input tags in second td and second input in third td; //这个表有两行,每行包含一个,第一个td中有一个select,第二个td中有一个输入标签,第三个td中有第二个输入;

<table id="tableID" class="table table-condensed">
       <thead>
          <tr>
             <th><label>From Group</lable></th>
             <th><label>To Group</lable></th>
             <th><label>Level</lable></th>

           </tr>
       </thead>
         <tbody>
           <tr id="rowCount">
             <td>
               <select >
                 <option value="">select</option>
                 <option value="G1">G1</option>
                 <option value="G2">G2</option>
                 <option value="G3">G3</option>
                 <option value="G4">G4</option>
               </select>
            </td>
            <td>
              <input type="text" id="" value="" readonly="readonly" />
            </td>
            <td>
              <input type="text" value=""  readonly="readonly" />
            </td>
         </tr>
         <tr id="rowCount">
          <td>
            <select >
              <option value="">select</option>
              <option value="G1">G1</option>
              <option value="G2">G2</option>
              <option value="G3">G3</option>
              <option value="G4">G4</option>
           </select>
         </td>
         <td>
           <input type="text" id="" value="" readonly="readonly" />
         </td>
         <td>
           <input type="text" value=""  readonly="readonly" />
         </td>
      </tr>
  </tbody>
</table>

  <button type="button" class="btn btn-default generate-btn search-btn white-font border-6 no-border" id="saveDtls">Save</button>


//call on click of Save button;
 $('#saveDtls').click(function(event) {

  var TableData = []; //initialize array;                           
  var data=""; //empty var;

  //Here traverse and  read input/select values present in each td of each tr, ;
  $("table#tableID > tbody > tr").each(function(row, tr) {

     TableData[row]={
        "fromGroup":  $('td:eq(0) select',this).val(),
        "toGroup": $('td:eq(1) input',this).val(),
        "level": $('td:eq(2) input',this).val()
 };

  //Convert tableData array to JsonData
  data=JSON.stringify(TableData)
  //alert('data'+data); 
  });
});

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

相关问题 如何使用JQuery迭代表行并访问一些单元格值? - How to iterate a table rows with JQuery and access some cell values? 如何遍历Bootstrap表并使用JQuery输入值 - How to iterate through a Bootstrap table and enter in values using JQuery Javascript:如何遍历值数组并创建表行 - Javascript: How to iterate through an array of values and create table rows jQuery:遍历所有表行并显示具有特定类的每个单元格的弹出窗口 - JQuery: Iterate through all table rows and display popup for each cell with a certain class 如何遍历HTML表列并使用JQuery将值输入到Javascript数组中 - How to iterate through an HTML table column and input the values into a Javascript array using JQuery 使用JavaScript迭代表格以获取特定单元格的值 - Iterate through a table using JavaScript to get values of specific cells 想用 Puppeteer 刮桌子。 如何获取所有行,遍历行,然后为每一行获取“td”? - Want to scrape table using Puppeteer. How can I get all rows, iterate through rows, and then get "td's" for each row? 如何使用 Jquery 获取表的所有行值? - How to get all the rows values of Table using Jquery? jQuery使用表对象迭代表行 - jQuery iterate table rows using table object 使用jquery遍历html表行以获取div id <td> 细胞 - iterate through html table rows with jquery to get div id inside <td> cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM