gpt4 book ai didi

javascript - 使用当前行从 html 表格单元格内的文本框中读取值

转载 作者:行者123 更新时间:2023-11-28 07:54:56 26 4
gpt4 key购买 nike

我使用 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();

但是浏览器返回未捕获类型错误:无法读取 null 的属性“单元格”

最佳答案

您已将 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();

关于javascript - 使用当前行从 html 表格单元格内的文本框中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com