我有一个 PHP-AJAX 脚本来从表中删除现有的财务行。 PHP 脚本工作正常并且行正在被删除。但问题出在 AJAX
调用的 success
函数内部。首先让我给你看一下表格:
忘记删除付款按钮,专注于删除项目。单击它时,我想删除所有已付款的项目,并且一切正常,直到我需要更新 Total
、Still to Pay
和 总付费
。
Total
单元格已正确更新,但我无法更新其他单元格。我将在其中看到 NaN $
。
这是脚本:
success:function(resp)
{
var pay_total = 0;
row.fadeOut(700);
//updating the total TD:
var totalVal = parseInt($("#final_cash").text());
var NewTotal = totalVal - parseInt(proj_cost);
$("#final_cash").text(NewTotal +' $');
//$.each because I have multiple rows returned from PHP
//and each row represent a payment in a different date.
$.each(resp, function(key, row)
{
$("#"+row.debt_id).fadeOut(600);
//Updating the still to pay TD:
var pay_total = parseInt(pay_total) + parseInt(row.payment);
console.log(pay_total);//The result is NaN
var still_cash_t = parseInt($("#still_cash").text());
var sct = pay_total + still_cash_t;
$("#still_cash").text(sct + ' $');
//Updating the Total Paid TD:
var paid_cash_t = parseInt($("#paid_cash").text());
var pct = paid_cash_t - pay_total;
$("#paid_cash").text(pct +' $');
});
},
最佳答案
删除var
:
var pay_total = parseInt(pay_total) + parseInt(row.payment);
避免在 function(key, row) {...}
内重新声明变量 pay_total
。
关于javascript - 无法使用 AJAX 和 PHP 返回的 JSON 数组更新表 TD 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186995/