gpt4 book ai didi

javascript - jQuery .each 函数不保留全局变量

转载 作者:行者123 更新时间:2023-11-28 12:21:18 24 4
gpt4 key购买 nike

我有一个函数,我希望运行 .each 循环并返回值的运行总计

在我的 .each 函数末尾,amountSold 变量为 0,并且在 .each 循环期间,它显示为未定义。我做错了什么?

function processChange(currentTarget) {
var amountSold = 0; //VARIABLE DECLARED
//limit these operations to the currentlySelected tab
var availableFundContainer = $(currentTarget).closest(".available-content").parent();

var fundBeingSold = $(availableFundContainer.children(0)).attr('data-investment-code');

availableFundContainer.find('.available-handler').each(function (index, val) {
var origVal = $(this).attr('data-original-value');
var currentSliderVal = $(this).next('.available-content').find('.slider').slider("value");
var amountSold = amountSold + (origVal - currentSliderVal); //PROBLEM LINE!!!!!
....}

最佳答案

您正在each回调函数内创建一个本地范围的变量,也称为amountSold。

替换

var amountSold = amountSold + (origVal - currentSliderVal); //PROBLEM LINE!!!!!

amountSold += (origVal - currentSliderVal); //PROBLEM LINE!!!!!

关于javascript - jQuery .each 函数不保留全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703855/

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