简体   繁体   English

jQuery:遍历2个特定单元格的表行

[英]JQuery: Iterating through table rows for 2 specific cells

I am a total JQuery newbie and I have been researching this for the past 2 days but my brain is now fried. 我是JQuery的新手,过去2天我一直在研究它,但是现在我的大脑已经炸了。

Here is what I have: 这是我所拥有的:

A table that has a certain amount of rows (like an online store's cart) 具有一定行数的表(例如在线商店的购物车)

I do not want the last row (since that contains cart total) and I do not want the first row (which are the column headers) 我不想要最后一行(因为它包含购物车总计),也不想要第一行(它们是列标题)

Here is what I want: 这是我想要的:

On document load, I want to go through each row of that table (except first and last) and grab (for example) the first column and last column, pass those values to a web service, and have the web service return a value to put in another column's text box of that row 在文档加载时,我要遍历该表的每一行(第一和最后一个除外),并获取(例如)第一列和最后一列,将这些值传递给Web服务,并让Web服务将值返回给放在该行的另一列的文本框中

the table structure is as follows: 表结构如下:

<table id="blahzayblahgrdItems">
<tbody>
<tr>
<td>product</td>
<td>req.ship date</td>
<td>est.ship date(input textbox)</td>
<td>price</td>
<td>qty</td>
<td>subtotal</td>
</td>
</tbody>
</table>

I'm trying to grab the values from the Product column and the QTY column to pass to webservice and populate the EST. 我正在尝试从“产品”列和“数量”列中获取值,以传递给Web服务并填充EST。 Ship Date column with whats returned from the web service 发货日期列以及从Web服务返回的内容

In code, I've tried something like 在代码中,我尝试了类似

$(function()
{
var partID;
var qty;

$("table[id*=grdItems] tbody tr").each(function() {})
)

However, I'm drawing a blank on getting the actual quantity and product to pass to my webservice. 但是,在将实际数量和产品传递给我的Web服务方面,我是一个空白。 I've tried different variations of code and throwing an alert box to see whats getting returned but I'm seeing headers, pieces of the table data, the full table data, etc. Any help would be GREATLY appreciated. 我尝试了不同的代码变体,并抛出一个警告框以查看返回的内容,但我看到的是标题,表数据片段,完整的表数据等。任何帮助将不胜感激。

Set a class for each row you want to parse and do this: 为要解析的每一行设置一个类,然后执行以下操作:

var partID = $("tr.some_class td")[4].text();
var qty = $("tr.some_class td")[5].text();

I think it would be a little easier to put a class (like info ) on the first and last rows (especially for styling) and do the following. 我认为将类(如info )放在第一行和最后一行(尤其是用于样式设置)并执行以下操作会稍微容易一些。

Updated jsfiddle with working example using jsfiddle's json echo: 使用jsfiddle的json回显更新了jsfiddle的工作示例:

http://jsfiddle.net/lucuma/fNwvX/3/ http://jsfiddle.net/lucuma/fNwvX/3/

// add a class to first and last tr to make formatting a little nicer as well
$('#blahzayblahgrdItems tr:first,#blahzayblahgrdItems tr:last').addClass('info');
// loop over each one except the ones with class
$('#blahzayblahgrdItems tr:not(.info)').each(function() {
    var $prod = $('td:eq(0)', this);
    var $sub = $('td:eq(5)', this);

    var result = $prod.text() + '-' + $sub.text();     // do your ajax with these values
    $.get('ajax/test.html', function(data) {
      $prod.text(data.val1);  // depends on your system
      $sub.text(data.val2);   // depends on your system
    });



});​

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

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