【问题标题】:How to add the result of 2 functions together and add result to div in jQuery如何将2个函数的结果加在一起并将结果添加到jQuery中的div
【发布时间】:2021-08-14 21:12:16
【问题描述】:

我有两个函数返回整数 finSum()getTotal()

我如何对结果求和并使用$(".grandTotal").text(somevalue); 之类的东西将其发送到<div class="grandTotal"></div>

【问题讨论】:

  • 是这个意思吗? $(".grandTotal").text(finSum()+getTotal());

标签: javascript jquery function


【解决方案1】:

只需将两个返回值相加即可:

var somevalue = finSum() + getTotal();

$(".grandTotal").text(somevalue);

【讨论】:

【解决方案2】:

箭头函数提供隐式返回:在不使用 return 关键字的情况下返回值。

您可以使用模板文字在div 上显示您的结果。

这是一个使用箭头函数和模板文字的完整示例。

let a = 2,
    b = 3,
    c = 4,
    d = 5

const finSum = () => a + b;
const getTotal = () => c + d;
const grandTotal = () => finSum() + getTotal();

let html = `<span>Gand Total: ${grandTotal()}</span>`;
$('.grandTotal').html(html);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="grandTotal">
</div>

【讨论】:

    猜你喜欢
    • 2023-01-14
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多