无法在Javascript或Jquery中循环遍历表的行

时间:2015-08-14 14:05:34

标签: javascript jquery

互联网上有很多例子,包括SO,告诉人们如何遍历表的行并使用Javascript或Jquery获取值。不幸的是,这些例子都不适合我:

JavaScript的:

var table = document.getElementById("tbInvoiceDetails");

var rowLength = table.rows.length;

for (var i = 0; i < rowLength; i += 1) {
    var row = table.rows[i];

    var cell = row.cells[10].innerHTML;
}

这得到:

<input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].Vat" value="0" type="hidden">0

如何从中获取值(= 0)?

JQuery的:

$("#tbInvoiceDetails tr").each(function () {
//Code
}

这根本行不通。我已经尝试过我能想到的所有组合&#34;&#34;没有任何作用。

一行表格HTML:

<tbody id="tbInvoiceDetails">
  <tr id="trInvoiceDetail0">
    <td style="display:none">
      <input name="InvoiceDetails.Index" value="0" type="hidden"></td>
    <td style="display:none"><input name="InvoiceDetails[0].id" value="-1" type="hidden"></td>
    <td style="display:none"><input name="InvoiceDetails[0].InvoiceId" value="0" type="hidden"></td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].LineTypeId" value="1" type="hidden">1</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].AllocationCodeId" value="19" type="hidden">2030 6016750 KQ73020394 11014008</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].GspId" value="" type="hidden"></td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].RunTypeId" value="" type="hidden"></td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].BillingPeriodFromDate" value="06/08/2015" type="hidden">06/08/2015</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].BillingPeriodToDate" value="19/08/2015" type="hidden">19/08/2015</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].Net" value="9999" type="hidden">9999</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].Vat" value="0" type="hidden">0</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].InterestPay" value="0" type="hidden">0</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].InterestReceiveable" value="0" type="hidden">0</td>
    <td><input id="InvoiceDetails_0__Title" name="InvoiceDetails[0].VatCodeId" value="8" type="hidden">8</td>
    <td><input class="btn" id="btnRemoveInvoiceDetail" value="Remove" onclick="removeRow(0);" type="button"></td>
  </tr>
</tbody>

1 个答案:

答案 0 :(得分:0)

走向列位置路线:

jQuery的:

$('#tbInvoiceDetails tr td:nth-child(11) input').each(
  function() {
    var val = this.value;

    console.log(val);
  }
);

Pure JS:

var table = document.getElementById("tbInvoiceDetails");

var rowLength = table.rows.length;

for (var i = 0; i < rowLength; i += 1) {    
  var input = table.rows[i].cells[10].firstElementChild;

  var val = input.value;
  console.log(val);
}