亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從兩個不同的 ajax 結果中添加兩個數字變量

從兩個不同的 ajax 結果中添加兩個數字變量

慕森王 2022-10-13 14:32:43
我想添加兩個數字并計算兩個不同 ajax 調用的總數,以便我可以將總值附加到 dom $.ajax({            url: "@Url.Action("MonthlyReport")",            data: { id: id },              success: function (data) {                  var total = 0;                  for (var i = 0; i < data.length; i++) {             // Create our number formatter.                total += data[i].interestAmountPerMonth                }                  var formatter = new Intl.NumberFormat('en-US', {                      style: 'currency',                      currency: 'USD',                  });                  const totalAmountAccrued = formatter.format(total)                  $('#totalAmountAccrued').append(totalAmountAccrued)            },            error: function (req, status, error) {}          });第二個ajax如下     $.ajax({            url: "@Url.Action("GetAllLoan")",            data: { id: id },            success: function (result) {                var formatter = new Intl.NumberFormat('en-US', {                    style: 'currency',                    currency: 'USD',                });                const originalLoanAmount = formatter.format(result.originalLoanAmount);                const amountWrittenOff = formatter.format(result.amountWrittenOff);                            },            error: function (req, status, error) {            }        });我想實現這樣的事情let generalTotal = totalAmountAccrued + amountWrittenOff
查看完整描述

3 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您可以使用 $.when 組合多個請求 https://api.jquery.com/jquery.when/


查看完整回答
反對 回復 2022-10-13
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

在 ajax 調用上方使用let聲明 totalAmountAccrued 和 amountWrittenOff 。

比如讓 totalAmountAccrued;

const totalAmountAccrued從和中刪除 const const amountWrittenOff。


查看完整回答
反對 回復 2022-10-13
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

解決所有承諾時使用Promise.allwhich triggersthen


function makeFetch(url, data) {

    return $.ajax({url: url, data: data})

}


Promise.all([

    makeFetch('@Url.Action("MonthlyReport")', {id: id}),

    makeFetch('@Url.Action("GetAllLoan")', {id: id})

])

.then(([monthlyReport, allLoan]) => {

    const totalAmountAccrued = monthlyReport.reduce((sum, data) => sum + data.interestAmountPerMonth, 0)

    const amountWrittenOff = allLoan.amountWrittenOff

    const overallTotal = totalAmountAccrued + amountWrittenOff

})


查看完整回答
反對 回復 2022-10-13
  • 3 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號