簡體   English   中英

使用當前行從html表格單元格內的文本框中讀取值

[英]read value from textbox inside html table cell using current row

我使用for循環將數據追加到表

for (var i = 0; i < data.length; i++) {
                date = data[i].inv_Date;
                invDate = date.substring(0, 10);
                billNo = data[i].Bill_No;
                netAmt = parseFloat(data[i].Net_Amt).toFixed(2);
                paidAmt = parseFloat(data[i].Paid_Amt).toFixed(2);
                balance = (parseFloat(netAmt) - parseFloat(paidAmt)).toFixed(2); //id = "damt['+i+']"
                $("#invoiceDetailTbl tbody").append("<tr id=" + i + ">" + "<td>" + invDate + "</td>" + "<td>" + billNo + "</td>" + "<td>" + netAmt + "</td>" + "<td>" + paidAmt + "</td>" + "<td>" + '<input type="text" class="discountAmt form-control input-sm" style="width: 100px;" placeholder="Discount Amt" id="damt" name="damt' + i + '">' + "</td>" + "<td>" + '<input type="text" class="payingAmt form-control input-sm" style="width: 100px;" placeholder="Paying Amt" id="pamt">' + "</td>" + "<td>" + balance + "</td>" + "<td>" + '<span class="glyphicon glyphicon-trash"></span>' + "</td>" + "</tr>");
                totalAmt = totalAmt + parseFloat(netAmt);
                totalBalance = totalBalance + parseFloat(balance);
            }

我正在使用讀取文本框的值

var PayingAmtValue = $(this).closest('tr').children('td.discountAmt').val();

但總是獲得空值。 我也會嘗試使用

var table = document.getElementById('invoiceDetailTbl');
        var DiscountAmtValue = $(this).val();
        var rowId = $(this).closest('tr').attr('id');
        PayingAmtValue = $(table.rows.item(rowId + 1).cells[5]).find('input').val();

但瀏覽器返回未捕獲的TypeError:無法讀取null的屬性“ cells”

您已將discountAmt應用於input而不是td 您必須找到在td內部具有class="discountAmt" input

// this will find children 'td' having class="discountAmt"
var PayingAmtValue = $(this).closest('tr').children('td.discountAmt').val();

將上面的代碼更改為

// this will find input with 'class="discountAmt"'
var PayingAmtValue = $(this).closest('tr').find('input.discountAmt').val();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM